기은P
시간이 멈추는 장소
기은P
  • Programming (272)
    • 개발노트 (1)
    • FrontEnd (56)
      • ES&JS 문법 (14)
      • HTML&CSS (4)
      • React 기본 (18)
      • React 심화 (12)
      • React 이슈 (2)
      • Project 연습 (1)
      • Next.js (5)
    • Backend&Devops (33)
      • AWS (2)
      • Docker (9)
      • Jenkins (6)
      • Nginx (6)
      • Node.js (1)
      • ElasticSearch (5)
      • 프레임워크&아키텍처 (2)
      • 암호화 (0)
      • 기타 (2)
    • 알고리즘 (3)
    • C# (8)
      • WPF (8)
    • Java (51)
      • 순수 Java (18)
      • RDF&Jena (12)
      • RCP&GEF (9)
      • JMX (5)
      • JMapper (3)
      • 오류해결 (4)
    • Database (21)
      • RDBMS (9)
      • NoSQL (2)
      • TSDB (1)
      • GraphQL (1)
      • Hibernate (3)
      • 데이터베이스 이론 (4)
      • Redis (1)
    • 프로토콜 (11)
      • Netty (4)
      • gRPC (5)
      • 프로토콜 개념 (2)
    • Server (4)
      • Linux (4)
    • 2020 정보처리기사 필기 (43)
      • 목차 (1)
      • 기출문제 (1)
      • 1과목 - 소프트웨어 설계 (6)
      • 2과목 - 소프트웨어 개발 (7)
      • 3과목 - 데이터베이스 구축 (8)
      • 4과목 - 프로그래밍 언어 활용 (7)
      • 5과목 - 정보시스템 구축 관리 (10)
    • 2020 정보처리기사 실기 (31)
      • 목차 (4)
      • 기출예상문제 (19)
      • 실기요약 (8)
    • 빅데이터분석기사 필기 (4)
      • 목차 (0)
      • 필기 요약 (3)
    • 전기 공학 (1)
      • CIM (1)
    • 산업자동화시스템 (3)
      • SCADA (1)
      • OPC UA (2)
    • 디자인패턴 (1)
    • 휴지통 (0)

공지사항

  • 공지사항/포스팅 예정 항목

최근 댓글

최근 글

전체 방문자
오늘
어제

티스토리

hELLO · Designed By 정상우.
기은P

시간이 멈추는 장소

[JAVA] JMX 예제 코드 및 jmxremote properties 설정
Java/JMX

[JAVA] JMX 예제 코드 및 jmxremote properties 설정

2020. 3. 18. 16:37
반응형
1) JMX 예제 코드

 

Main

public class MBeansTest {
	public static void main(String[] args) throws MalformedObjectNameException, InstanceAlreadyExistsException,
			MBeanRegistrationException, NotCompliantMBeanException, InterruptedException {
		MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();

		ObjectName name = new ObjectName("Hello:type=Hello");

		Hello mbean = new Hello();

		mbs.registerMBean(mbean, name);

		System.out.println("Waiting...");
		Thread.sleep(Long.MAX_VALUE);
	}
}

 

 

MBean

public interface HelloMBean {
	public void sayHello();
	public int add(int x, int y);
	
	public String getName();
	
	public int getCacheSize();
	public void setCacheSize(int Size);
}

 

 

Hello

import javax.management.AttributeChangeNotification;
import javax.management.MBeanNotificationInfo;
import javax.management.Notification;
import javax.management.NotificationBroadcasterSupport;

public class Hello extends NotificationBroadcasterSupport implements HelloMBean {
	public void sayHello() {
		System.out.println("hello, world : " + name);
	}

	public int add(int x, int y) {
		return x + y;
	}

	public String getName() {
		return this.name;
	}

	public int getCacheSize() {
		return this.cacheSize;
	}

	public synchronized void setCacheSize(int size) {
		int oldSize = this.cacheSize;
		this.cacheSize = size;

		System.out.println("Cache size now " + this.cacheSize);

		// 현재 CacheSize와 이전 CacheSize에 관련된 알림 객체를 생성함
		// Attribute에 해당하는 변수들의 데이터에 변경이 일어날 경우 알려줌.
		Notification n = new AttributeChangeNotification(this, sequenceNumber++, System.currentTimeMillis(),
				"CacheSize changed", "CacheSize", "int", oldSize, this.cacheSize);
		// 알림 객체를 등록
		sendNotification(n);
	}

    @Override 
    public MBeanNotificationInfo[] getNotificationInfo() { 
        String[] types = new String[] { 
            AttributeChangeNotification.ATTRIBUTE_CHANGE 
        }; 
        String name = AttributeChangeNotification.class.getName(); 
        String description = "An attribute of this MBean has changed"; 
        MBeanNotificationInfo info = 
            new MBeanNotificationInfo(types, name, description); 
        return new MBeanNotificationInfo[] {info}; 
    } 

	private final String name = "Reginald";
	private int cacheSize = DEFAULT_CACHE_SIZE;
	private static final int DEFAULT_CACHE_SIZE = 200;
    private long sequenceNumber = 1; 
}

 

 

풀이

  • MBeans에서 Attribute와 Operations에 속하는 Get,Set하는 메소드와 추가적인 기능에 대해 추상적으로 정의하고, Hello 에서는 추상으로 정의된 기능을 구체적으로 작성한다. 
  • 이름과 x,y 변수의 값을 받아 이름을 출력하고 x와 y의 값을 더하는 단순한 기능을 구현했고, 추가적으로 중요하게 보아야 하는 것은 CachSize라는 임의의 숫자를 정의해서 현재 값이 변할 때 변했다라는 것을 알려주기 위한 알림을 등록했다는 것이다.
  • 이를 위해서 NotificationBroadcasterSupport를 상속받았고, getNotificationInfo를 오버라이드를 해야 한다. Attribute의 데이터가 변경될 경우의 행위를 오버라이드하고, 이 알림 객체를 Set메소드를 통해 등록했다.
  • Mbs 서버, 즉 MBeanServer를 실행시키고 JConsole 같은 것으로 MBean에 등록한 함수를 실행시키면 해당 함수가 작동하는 것을 확인해 볼 수 있다.
  • JConsole의 위치  :  JDK 폴더 – bin – jconsole

 

2) 서버 실행 전 properties 세팅

 

  • JMX에서 MBeanServer를 실행시키려면 jmxremote파일을 수정해야 한다.
  • JDK 폴더 – jre – lib – management 에 들어가서 jmxremote.password.template 파일과 management.properties을 수정하도록 한다.
  • jmxremote.password.template에서는 맨~ 밑에 있는 저 두 줄의 주석을 지워버리고 jmxremote.password 이름으로 저장한다.

 

 

 

 

  • management.properties도 맨~ 밑에 가서 아래의 내용을 추가하도록 한다. Port는 자유롭게 변경해도 무관하다. 이 파일은 그냥 이대로 저장.

com.sun.management.jmxremote

com.sun.management.jmxremote.port=7777

com.sun.management.jmxremote.ssl=false

com.sun.management.jmxremote.password.file=jmxremote.password

com.sun.management.jmxremote.access.file=jmxremote.access

com.sun.management.jmxremote.authenticate=false

 

 

 

 

  • 그리고 management 폴더에서 CMD창을 열고 2개의 파일의 열람권한을 F(모두허용)로 바꿔준다

 

cacls jmxremote.access /P 컴퓨터이름:F

cacls jmxremote.password /P 컴퓨터이름:F

 

 

 

3) 실행 결과

 

 

 

 

반응형
저작자표시 변경금지 (새창열림)

'Java > JMX' 카테고리의 다른 글

[오류해결] javax.management.NotCompliantMBeanException:  (0) 2020.08.24
[Java] JMX MBean을 이용한 모니터링(Client, Server)  (0) 2020.05.08
[Java] JMX Connector의 Server, Client 활용  (0) 2020.03.18
[JAVA] JMX의 mBean이란? JMX는 무엇인지?  (0) 2020.03.18
    'Java/JMX' 카테고리의 다른 글
    • [오류해결] javax.management.NotCompliantMBeanException:
    • [Java] JMX MBean을 이용한 모니터링(Client, Server)
    • [Java] JMX Connector의 Server, Client 활용
    • [JAVA] JMX의 mBean이란? JMX는 무엇인지?
    기은P
    기은P
    기은P의 블로그 일상과 개발 관련 포스팅 #React #Typescript #Next #Nest https://github.com/kimdongjang

    티스토리툴바