DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Frameworks
  4. Spring Boot and Working With MBeans [Snippet]

Spring Boot and Working With MBeans [Snippet]

Check out this example code on working with MBeans in your Spring Boot projects.

Pardeep Kumar user avatar by
Pardeep Kumar
·
Jan. 25, 19 · Code Snippet
Like (9)
Save
Tweet
Share
27.81K Views

Join the DZone community and get the full member experience.

Join For Free

The following example demonstrates how one can manage object operation remotely via the JMX Console. Once you declare your object as MBeans, one can extend the example to apply remote logging without changing log4j, which is very useful in production environments, evicting cache or any other operation that needs to be handled remotely via MBean/JMX.

 @ManagedResource: This Spring annotation applies on the Class Level, and this will declare it as an MBean.

 @ManagedOperation: Apply this on the method level and the method will be shown as an available operation in the JMX console for the MBean.

If there is any input param you want to pass to operation/as in your method, you can use the @ManagedOperationParameters and  @ManagedOperationParameter annotations.

Below is the sample Class declared as MBean:

@ManagedResource(
objectName="PD:category=MBeans,name=testBean",
description="Managed Bean")
@Component("testMbean")
public class TestMbean {

private String message = "Simple Message";

public TestMbean(){
System.out.println("......TestMbean........");
}

@ManagedOperation
public void resetMessageViaMBean(){
this.message = "Message RESET";
}

public void show(){
System.out.println(message);
}

}


Below is the test Client, written as:

public static void main(String[] args) {
ApplicationContext applicationContext = SpringApplication.run(ExamplesApplication.class, args);

TestMbean obj = (TestMbean) applicationContext.getBean("testMbean");
obj.show();
try{
//Sleep for 2 min 
//Open JConsole and select your process Bean click PD > testBean > Operation > resetMessageViaMBean
//Click on resetMessageViaMBean - if you see testMbean - it is resetting message
//Check whats the message after sleep time is over
Thread.sleep(120000); 
}catch(Exception e){

}

obj.show();

}


You can download the code from our Git repository.

Once you run the code, open the JConsole and select your process Bean click PD > testBean > Operation > resetMessageViaMBean. Click on resetMessageViaMBean. If you see testMbean, it is resetting the message. Then, check the message after sleep time is over.

/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.2.RELEASE)


......TestMbean........
2019-01-23 15:51:31.394 INFO 7984 --- [ main] c.p.e.examples.ExamplesApplication : Started ExamplesApplication in 0.982 seconds (JVM running for 1.324)
Simple Message
Message RESET


Happy coding!

Spring Framework Spring Boot

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • AWS Cloud Migration: Best Practices and Pitfalls to Avoid
  • How to Secure Your CI/CD Pipeline
  • Real-Time Stream Processing With Hazelcast and StreamNative
  • Upgrade Guide To Spring Data Elasticsearch 5.0

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: