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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

  • The Self-Hosting Math Changed While We Were Still Arguing About Sovereignty
  • Spring AI on Local LLMs
  • Microsoft Fabric Throttling: Diagnose Before You Scale
  • LangChainGo vs Genkit Go: Where Genkit Shines

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Jan. 25, 19 · Code Snippet
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
31.8K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook