This Blog entry is the first part of a two part series. I wanted to share knowledge gained on my current project. The project required that the application be written utilizing JBoss service technology. A JBoss service is a JMX (Java Management Extensions) compliant MBean. JBoss uses JMX as its component bus. JMX is a standard for managing and monitoring all varieties of software and hardware components from Java. An MBean is a Java object that implements one of the standard MBean interfaces and follows the associated design patterns. Enough background, let’s write a simple JBoss service. I’ll be using Eclipse 3.1 to create the service.
Create an Eclipse Project
As with any Eclipse project, we need to create a Java Project: blog-example. The project will be organized with the following directory structure:
· \src – Note: use src as the project's source directory.
· \dist
· \lib
· \resources\META-INF
Create Project Packages
Create the following project package under the src directory:
· blog.example.services
Create Support Files
The project will utilize Apache Ant (http://ant.apache.org/) to build and deploy the project. You will need an Ant build.properties file in the blog-example directory. The file sets the jboss.home and jboss.config parameters. My JBoss installation is located at C:/jboss-4.0.3SP1. Also, I use the default JBoss server configuration (C:\jboss-4.0.3SP1\server\default). Therefore, my build.properties file looks like this:
jboss.home=C:/jboss-4.0.3SP1
jboss.config=default
Next, create an Ant build.xml file in the blog-example directory. Copy the build.xml text from Appendix A.
Finally, create a jboss-service.xml file under the blog-example\resources\META-INF directory. For now, the file should just include the following text:
<?xml version="1.0" encoding="UTF-8"?>
<server>
</server>
Library Files
There are several libraries that we will import into the project. Once copied, we will add the jar files into the project. First, copy jboss-system.jar, jboss-jmx.jar, and commons-logging.jar files from the jboss.home\lib to the blog-example\lib directory. Next, copy the scheduler-plugin.jar from jboss.home \server\default\lib to the blog-example\lib directory. Finally, use the blog-example project properties dialog to add the jar files that were copied into the blog-example\lib into the Java Build Path for the project.
============================================
Tip: Remember to refresh your project before you modify the project properties. Otherwise, the jar files will not be available to add to the project’s build path
============================================
Create an MBean Interface
Now it is time to create the MBean interface. Select the blog.example.services package and create an interface called BlogExampleServiceMBean that extends the abstract interface org.jboss.system.ServiceMBean. The ServiceMBean interface is located in the jboss-system.jar file. This interface exposes the lifecycle methods, and a couple of other informational attributes for state and name.
Create a ServiceMBeanSupport Class
After the MBean interface is coded, create a class, in the blog.example.services package, called BlogExampleService that extends org.jboss.system.ServiceMBeanSupport and implements the MBean interface you just created: BlogExampleMBean. Finally, use Eclipse to add the unimplemented methods. The following methods will be added:
getName()
getState()
getStateString()
jbossInternalLifecycle(String arg0)
create()
start()
stop()
destroy()
preRegister(MBeanServer arg0, ObjectName arg1)
postRegister(Boolean arg0)
preDeregister()
postDeregister()
To discover the lifecycle calls for the service, we will use log statements within the implemented methods. Add the following class definition to the BlogExampleService class:
private static Log _log = LogFactory.getLog(BlogExampleService.class.getName());
Now add logging information to all the methods like follows:
_log.info("BlogExampleService ============ methodName()");
==============================================
Warning: If the service class (ServiceMBeanSupport) is called <servicename>, the interface must be called <servicename>MBean. Otherwise, you will receive a Class does not expose a management interface exception.
==============================================
Modify the jboss-service.xml file
It is time to add the BlogExampleService description in the jboss-service.xml. The file is located under the blog-example\resources\META-INF directory. Add the following entry within the <server> tags:
<mbean code="blog.example.services.BlogExampleService"
name="blog.example:service=BlogExampleService">
</mbean>
The code value is the full service class name. The name is a reference to the service. This reference will be show up in the JMX-Console once we deploy the SAR file into the JBoss Application Server.
Set the Name of the Service
We must override the getName() method for the service class…
public String getName() {
_log.info("BlogExampleService ============ getName()");
return "blog.example:service=BlogExampleService";
}
This name must match the name attribute set in the jboss-service.xml.
Start JBoss Applicaiton Server
Before the service is deployed, start JBoss
Create and Deploy the SAR File
The project file needs to be complied and archived into a SAR File.
The JBoss documentation describes the SAR File:
A SAR file is essentially a jar file which gets interpreted by JBoss in a slightly different manner. This file should have all the source code for your MBean and the service (it may also contain references to other jar files in its META-INF) and a jboss-service.xml file inside META-INF directory. The jboss-service.xml file should have all the information about the service and required attributes.
Fortunately, the ANT build.xml file automates compiling, creating, and deploying the SAR File. Run the deploy target, you should see the following output in the Eclipse console:
Buildfile: C:\psBlogs\blog-example\build.xml
compile:
[mkdir] Created dir: C:\psBlogs\blog-example\build
[javac] Compiling 2 source files to C:\psBlogs\blog-example\build
dist:
[copy] Copying 1 file to C:\psBlogs\blog-example\build
[jar] Building jar: C:\psBlogs\blog-example\dist\blog_example.sar
deploy:
[copy] Copying 1 file to C:\jboss-4.0.3SP1\server\default\deploy
BUILD SUCCESSFUL
Total time: 3 seconds
Test the Service
Once the SAR is deployed. The JBoss console displays the following output fragment from the log output added to each method:
BlogExampleService ================= preRegister()
BlogExampleService ================= postRegister()
BlogExampleService =============== jbossInternalLifecycle()
BlogExampleService =============== jbossInternalLifecycle()
BlogExampleService ================= getState()
Start the JBoss Console
Enter http://localhost:8080/ in the browser. Select the jmx-console hyperlink under the JBoss Management category. The JMX Agent View page will display the registered service domains; including the blog.example domain.
Select the service=BlogExampleService link under the blog.example domain. Once selected, the JBoss console will display the following output fragment for the log output added to each method:
BlogExampleService =================== getStateString()
BlogExampleService ======================= getState()
BlogExampleService ====================== getName()
Finally, select the Invoke Button under the start() operation to execute the start method of the BlogExampleService. The JBoss console will display the log output from the start() method of our service:
BlogExampleService ========================= start()
What’s Next?
In Part I of this Blog, we created a simple JBoss service and invoked service methods from the JBoss JMX-Console. In Part II, we will extend this service to use other JBoss services.
Appendix A – Ant build.xml file
<?xml version="1.0"?>
<project name="blog_example" default="deploy" basedir=".">
<property file="build.properties"/>
<path id="classpath">
<pathelement location="build"/>
<fileset dir="${basedir}/lib">
<include name="*.jar"/>
</fileset>
<fileset dir="${jboss.home}/lib">
<include name="*.jar"/>
</fileset>
<fileset dir="${jboss.home}/server/${jboss.config}/lib">
<include name="*.jar"/>
</fileset>
<fileset dir="${jboss.home}/server/${jboss.config}/deploy">
<include name="*.jar"/>
<include name="*.sar"/>
</fileset>
</path>
<path id="client.classpath">
<path refid="classpath"/>
</path>
<target name="compile" description="Compile the source">
<mkdir dir="build"/>
<javac destdir="build"
debug="on"
deprecation="on"
optimize="on"
classpathref="classpath">
<src path="src"/>
</javac>
</target>
<target name="dist" depends="compile" description="Package the application">
<copy todir="build">
<fileset dir="resources"/>
</copy>
<mkdir dir="dist"/>
<jar jarfile="dist/${ant.project.name}.sar" basedir="build"/>
</target>
<target name="deploy" depends="dist" description="Deploy the application">
<copy todir="${jboss.home}/server/${jboss.config}/deploy">
<fileset dir="dist"/>
</copy>
</target>
<target name="undeploy" description="Undeploy the application">
<delete file="${jboss.home}/server/${jboss.config}/deploy/${ant.project.name}.sar"/>
</target>
<target name="clean" depends="undeploy" description="Cleanup">
<delete dir="dist"/>
<delete dir="build"/>
</target>
</project>
Comments
Nice article John.
Would like see more on same topics.
Thanks & Regards,
Vikas
Hi John,
Wonderful article. It was very clear and provided a step-by-step approach for a new-bie.
Continuing on your 2nd article.
Thanks
Shankar
Awesome !!! Really a fantastic article.
I am trying to write a startup class, could you pls send me the source code of BlogExampleServiceMBean.java
BlogExampleService.java. It will be a great help.
my email id mageshcando@gmail.com
Thanks in advance,
Magesh
I have to schedule my application to Run twice daily,
11AM and 3PM. How to configure for this. Below is the setting done just to set the schedular. Please help.
boss-4.0.2/server/default/deploy/blog_example.sar
org.jboss.deployment.DeploymentException: Domain part must be specified; - nes
d throwable: (javax.management.MalformedObjectNameException: Domain part must
specified)
at org.jboss.system.ServiceConfigurator.install(ServiceConfigurator.ja
:143)
at org.jboss.system.ServiceController.install(ServiceController.java:2
)
at sun.reflect.GeneratedMethodAccessor41.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcc
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispat
er.java:141)
at org.jboss.mx.server.Invocation.dispatch(Invocation.java:80)
at org.jboss.mx.server.Invocation.invoke(Invocation.java:72)
at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoke
java:249)
at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:644
at org.jboss.mx.util.MBeanProxyExt.invoke(MBeanProxyExt.java:177)
at $Proxy4.install(Unknown Source)
at org.jboss.deployment.SARDeployer.create(SARDeployer.java:220)
at org.jboss.deployment.MainDeployer.create(MainDeployer.java:918)
at org.jboss.deployment.MainDeployer.deploy(MainDeployer.java:774)
at org.jboss.deployment.MainDeployer.deploy(MainDeployer.java:738)
at sun.reflect.GeneratedMethodAccessor48.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcc
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispat
er.java:141)
at org.jboss.mx.server.Invocation.dispatch(Invocation.java:80)
at org.jboss.mx.interceptor.AbstractInterceptor.invoke(AbstractInterce
or.java:121)
This article was very useful in solving my problem with deploying a startup class in JBoss. They key thing was naming the startup class (service) and the implemented MBean interface as per conventions. Thanks again.
first of all, its good article in the category of kick start :)
the only issue i faced during deployment was:
build.xml was not including jboss-service.xml file into META-INF folder while creating SAR file.I put it by myself in META-INF folder.
Hi,
It is really a good article.
How do I use this service in future
How do I invoke it?
Could you give me an example?
and could you send the source code to me also.
Thanks a lot.
Thanks
Arif
Hi,
Great article about using jboss custom services.
Could you forward the source code to chengsuntx@hotmail.com so that I can follow to try?
Thanks in advance.
Frank
Normally, I don't put myself up for public criticism, but at John's request, I'm putting myself out there.
Anyway, in case this helps someone in the future. I mis-typed the package name when creating it as blog.sample.services (rather than example).
Unfortunately, this yielded a non-obvious error message from jboss which was: "Deployment FAILED resaon: No ClassLoaders found for: blog.example.services.BlogExampleServices".
Which was JBOSS's way of telling me that "blog.example.services.BlogExampleServices" didn't exist in the sar.
I, of course, wasted an immense amount of time figuring this out. Hope this helps someone in the future.
David
John,
This was very helpful specially to the people who are newbie to the JBoss Custom service.
John,
Thanks for the very helpful article. I was able to work through the example in part 1 and will tacke part 2 shortly. I just wanted to add some comments to help other newbies like me who may not know some of the things you take for granted.
1) Install Ant following their instructions:
2) In reference to "After the MBean interface is coded," - there is nothing additional you have to do after Eclipse creates it. The BlogExampleServiceMBean.java file only needs to contain:
public interface BlogExampleServiceMBean extends ServiceMBean {}3) In reference to "run the deploy target" - right-click on the build.xml file in the Eclipse Package Explorer and then click on "Run as" and select "1 Ant build".
4) To start the JBoss console on my system I had to leave off the ":8080/" port number and just use: http://localhost which may be a quirk of the JBoss setup that I am working with.
I hope this helps someone eventually as much as John's article has helped me,
DZ
This article is very useful. but i have one question
i need to start mbean service as jboss server up, that is without user have to manually up the service. how can i do that,
thank for this very good article and files you send me. Your article help me very much.
The example helped me to get a feel of Mbean and I can deploy and test it. Thanks a lot.
What i am tring to do is create 2 mbeans and one should pass an object to another mbean. The First mbean will have setter and getter methods. Any thoughts?
Just what I was looking for. It worked great.
Thx!
Wes
John
That was a great article to me as am just starting with custom services in JBOSS. would be great if you could send me the source file.
Thanks
Rajesh
This is really good article thanks
I have source code and run this successfully (janaka@datam.co.uk)
John,
Thanks for a very useful article. Would u send me the sample codes as well? TIA!
Rgds,
Jonathan
I would also like to have the source code for two java files mentioned in the article. My email address is rukesht@gmail.com
Thanks
Rukesh
John has generously offered to email the files in this article to anyone who wants them.
If you would like a copy of these files, please send him your request via email to john.howard@platinumsolutions.com.
hi,
My application is web based, now i want to turn the application to jboss service.In Service class where i need to change so that my application can run, i need help on this
Thanks........
hai,
this good aricle to get immediate understanding about building jboss custom services
Pls forward me the code for
BlogExampleServiceMBean.java
BlogExampleService.java
jboss-service.xml
thanks in advance
Hi,
Please give source code for:
BlogExampleServiceMBean.java
BlogExampleService.java
Nice article it is. But please send the files.
Thanks,
Hi,
This is really nice article Please help me to send BlogExampleServiceMBean.java and BlogExampleService.java
basusingh@gmail.com
Thanks in advance
Basu(Mail-ID: basusingh@gmail.com)
Hi,
It is really a good article.
But I got a question. How do I use this service after I created it ?
How do I invoke it?
Could you give me an example?
and could you send the source code to me also.
Thanks a lot.
Johnny
Please give source code for:
BlogExampleServiceMBean.java
BlogExampleService.java
Great article! But i can't do the code, the other things i can do. Please help!
Regards Karl
Karl,
Glad you enjoyed the article. Please send your email address and I will forward the files to you.
John
john.howard@platinumsolutions.com
could U send mi those files?
molboro@gmail.com
Thx!
Post new comment