JBoss AS 7
JBoss AS 7 was released in July 2011. I had worked on JBoss server 4 years ago – on JBoss 4.2.3 GA. For me, it was a complete shift in architecture of the JBoss. I couldn’t even start the server! So here I would explain step by step – how to start the server -to setting up JMS queue and topic – to creating clients.
The article is divided into 7 major chunks
- Setting up and running the JBoss AS 7.
- Setting up users.
- JMS Configuration files walkthrough
- Creating and Running Queue
- Creating and Running Topic
- Running the sample code
- Exceptions I encountered while preparing this article
We assume that you already have a fair idea on JMS before proceeding.
For this article, we shall use the default configurations provided by the server – so that it is easy to create.
Setting up and Running JBoss AS 7
- standalone.xml (default)
The default configuration for standalone server – standalone.xml doesnot have JMS enabled by default. So we will use standalone-full.xml.
Now its time to run the JBoss with our desired configuration.
Go to command prompt and reach “jboss-home\bin” folder. And fire up the following command.
And this starts our JBoss in standalone-full.xml.
Setting up users
- Management Realm (server administrators)
- Application Realm (application users)
For JMS, we need an application realm user with a role. We shall use “guest” role for example. We shall also need a management realm user to validate if our JMS is up and running.
To create a user, go to “jboss-home\bin”. Use command
This will add a user to the application. Create following 2 users
What type of user do you wish to add?
a) Management User (mgmt-users.properties)
b) Application User (application-users.properties)
Enter the details of the new user to add.
Realm (ApplicationRealm) :
Username : testuser
Re-enter Password :
What roles do you want this user to belong to? (Please enter a comma separated list, or leave blank for none) : guest
We need to follow the above mentioned steps for both the users.
JMS Configuration files – Walkthrough
- The user ‘roles’ permissions
- The RemoteConnectionFactory JNDI entry
- The Topic JNDI entry
- The Queue JNDI entry
The user ‘roles’ permissions
<permission roles=”guest” type=”send”>
This text takes us to lines –
<security-setting match="#"> <permission type="send" roles="guest"/> <permission type="consume" roles="guest"/> <permission type="createNonDurableQueue" roles="guest"/> <permission type="deleteNonDurableQueue" roles="guest"/> </security-setting>
Note the roles – they have been marked as guests. These roles must match the roles of user we have created. This is the reason why we created application user ‘testuser’ with role as guest.
The RemoteConnectionFactory JNDI Entry
Now search for text
We reach place where code looks like
<connection-factory name="RemoteConnectionFactory"> <connectors> <connector-ref connector-name="netty"/> </connectors> <entries> <entry name="RemoteConnectionFactory"/> <entry name="java:jboss/exported/jms/RemoteConnectionFactory"/> </entries> </connection-factory>
Check the code for the tag with name attribute as “java:jboss/exported/jms/RemoteConnectionFactory”. This is the JNDI name using which connection factory can be obtained. The JNDI name always starts with “java:jboss/exported/”. If we don’t give this, and pass the JNDI name as “jms/RemoteConnectionFactory”, the JBoss itself prepends “java:jbpss/exported/” to complete the JNDI name.
Remember – only “java:jboss/exported/” is prepended to the JNDI string if it doesn’t exist in the string. The whole name should resolve to the JNDI name. Otherwise the JNDI lookup fails.
The Topic JNDI Entry
<jms -topic=”-topic” name=”testTopic”></jms>
Upon search, we see entry as
<jms-topic name="testTopic"> <entry name="topic/test"/> <entry name="java:jboss/exported/jms/topic/test"/> </jms-topic>
Check out the tag with attribute name as “java:jboss/exported/jms/topic/test”. This is the JNDI name for the Topic. The short hand name for this topic is “jms/topic/test”.
Note: The shorthand name is added “java:jboss/exported/” in front by the JBoss server when we do a JNDI lookup.
The Queue JNDI Entry
A sample queue is already provided with the config file. Search for it in configuration file as
<jms -queue=”-queue” name=”testQueue”> </jms>
The JNDI name and other things are same as the ‘topic’ we discussed.
Creating and Running Queue
- The required JAR file
- The InitialContext creation
- The JNDI Lookup
- Creating Queue Sender
- Creating Queue Receiver
The required JAR file
The InitialContext creation
InitialContext is created so that we can perform JNDI lookups. This is required step for both the queue and topic coding. Following is the code:
// Create an initial context.
Properties props = new Properties();
InitialContext context = new InitialContext(props);
Note the properties Context.SECURITY_PRINCIPAL and Context.SECURITY_CREDENTIALS. These have the application user User ID and Password respectively. This user has the permissions as mentioned in the standalone-full.xml’s permissions configuration discussed earlier.
The JNDI Lookup
String factoryName = “jms/RemoteConnectionFactory”;
String queueName = “jms/queue/test”;
Queue queue = (Queue) context.lookup(queueName);
Creating Queue Sender
QueueConnection connection = factory.createQueueConnection(“testuser”, “password”);
QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
QueueSender sender = session.createSender(queue);
Creating Queue Receiver
Creating and Running Topic
Running the Sample Code
- Download the sample code from here: Download JBoss AS 7 JMS Sample Code
- Extract these files into a directory say Code. Recheck that the directory structure looks like Code\com\marshall\jms\queue and Code\com\marshall\jms\topic.
- Copy the jboss-home\bin\client\jboss-client.jar to folder Code\lib\jboss-client.jar
- Compile the sample code classes as
- javac -classpath lib\jboss-client.jar com\marshall\jms\queue\QSender.java
- javac -classpath lib\jboss-client.jar com\marshall\jms\queue\QReceiver.java
- javac -classpath lib\jboss-client.jar com\marshall\jms\topic\TPublisher.java
- javac -classpath lib\jboss-client.jar com\marshall\jms\topic\TSubscriber.java
- Run the queue classes as
- start java -classpath .;lib\jboss-client.jar; com.marshall.jms.queue.QSender
start java -classpath .;lib\jboss-client.jar; com.marshall.jms.queue.QReceiver
- Run the topic classes as
- start java -classpath .;lib\jboss-client.jar; com.marshall.jms.topic.TPublisher
start java -classpath .;lib\jboss-client.jar; com.marshall.jms.topic.TSubscriber
Exceptions I encountered during preparation of this article
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
javax.jms.JMSSecurityException: User: testuser doesn’t have permission=’CONSUME’ on address jms.queue.testQueue
javax.jms.JMSSecurityException: User: testuser doesn’t have permission=’SEND’ on address jms.queue.testQueue
This means the user whose credentials we are using does not have permissions over the JMS system. Check the “jboss-home\stabdalone\configuration\application-roles.properties” to see if the user we are using has the correct role. This role must match with the role that has permissions mentioned in “jboss-home\standalone\configuration\standalone-full.xml” file.
javax.jms.JMSSecurityException: Unable to validate user: testuser
Check if the user being passed when creating both initial context and the connection is correct. He should have already been created. If you created it now and it doesn’t work, try restarting the server.