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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Actuator Enhancements: Spring Framework 6.2 and Spring Boot 3.4
  • How Spring Boot Starters Integrate With Your Project
  • A Practical Guide to Creating a Spring Modulith Project
  • Structured Logging in Spring Boot 3.4 for Improved Logs

Trending

  • Software Delivery at Scale: Centralized Jenkins Pipeline for Optimal Efficiency
  • SaaS in an Enterprise - An Implementation Roadmap
  • How to Introduce a New API Quickly Using Micronaut
  • Go 1.24+ Native FIPS Support for Easier Compliance
  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.

By 
Pardeep Kumar user avatar
Pardeep Kumar
·
Jan. 25, 19 · Code Snippet
Likes (9)
Comment
Save
Tweet
Share
31.1K 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.

Related

  • Actuator Enhancements: Spring Framework 6.2 and Spring Boot 3.4
  • How Spring Boot Starters Integrate With Your Project
  • A Practical Guide to Creating a Spring Modulith Project
  • Structured Logging in Spring Boot 3.4 for Improved Logs

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!