In this example two servers will communicate to each other using JMS and JBoss. Messages will be posted to a topic where a listener will then receive the message (one sender, many listeners model).
There are three pieces to tackle in this example, a message sender, a message listener, and a JMS enabled application server (JBoss) which handles the receiving and sending of messages. To see this work in practice, you will need two boxes, one called ‘server’, and another called ‘sender’. You can run the example on one machine, but it is not as exciting. On ‘sender’, a java main will be executed to send messages. A java JMS listener and a single instance of JBoss will be running on ‘server’ to receive messages.
Messages are sent from the sender to the JBoss topic where they are then passed to listeners as events.
Install JBoss 4.x on ‘Server’:
Download JBoss at:
http://www.jboss.com/products/jbossas/downloads (go for the tar.gz)
Unpack and execute [JbossDIR]/bin/run.sh (run.bat on Windows), then go to http://yourhost:8080/ and you should get the JBoss start page. If that works, stop JBoss and fire up your java editor.
Create a new java project for the sender:
Add the following jar files into your project’s build path:
| Jboss-j2ee.jar |
[JbossDIR]\server\default\lib\ |
| jpnserver.jar |
[JbossDIR]\server\default\lib\ |
| jbossmq.jar |
[JbossDIR]\server\default\lib\ |
| jboss-common.jar |
[JbossDIR]\lib\ |
| concurrent.jar |
[JbossDIR]\lib\ |
The code: Sender.java (txt)
Change ‘server:1099’ in the main method to point to your JBoss server.
In the main method, a sender object is instantiated, and pointed to the
'example' topic located on the JMS server. It then sends a list of
Adam Sandler movies to the topic.
/////// Main:
//change the server url to your situtation:
Sender sender = new Sender("server:1099", "topic/example");
sender.send("Billy Madison 1995");
/////// initializing the sender (called from constructor):
private void initializeSender() throws JMSException, NamingException {
Properties props = new Properties();
props.setProperty("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory");
props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
props.setProperty("java.naming.provider.url", url_);
Context
context = new InitialContext(props);
TopicConnectionFactory tcf = (TopicConnectionFactory)
context.lookup("ConnectionFactory");
conn =
tcf.createTopicConnection();
topic =
(Topic) context.lookup(name_);
session =
conn.createTopicSession(false,
TopicSession.AUTO_ACKNOWLEDGE);
conn.start();
}
/////// the send method:
public void send(String text) throws JMSException, NamingException {
// Send a text msg
TopicPublisher send = session.createPublisher(topic);
TextMessage tm = session.createTextMessage(text);
send.publish(tm);
send.close();
}
Create a new java project for the listener:
Add the same .jar files to the project as listed for the sender.
The code: Listener.java (txt)
Note that the listener binds to localhost:1099 because it is running on the same machine as the JBoss JMS service.
The listener setup code is the same except in the main method, it creates a new Listener
object, which waits for its onMessage event to fire.
/////// Main:
Listener listener = new Listener("localhost:1099", "topic/example");
public void onMessage(Message msg) {
TextMessage tm = (TextMessage) msg;
try {
System.out.println("Incoming message: "
+ tm.getText());
} catch
(Exception e) {
e.printStackTrace();
}
}
Edit the JBoss JMS xml configuration file to add the topic:
JBoss needs to know about the 'example' topic. Edit
[JbossDIR]\server\default\deploy\jms\jbossmq-destinations-service.xml
and add the following xml under the <server> element:
<mbean code="org.jboss.mq.server.jmx.Topic"
name="jboss.mq.destination:service=Topic,name=example">
<depends
optional-attribute-name="DestinationManager">
jboss.mq:service=DestinationManager</depends>
</mbean>
This is just a basic configuration. JBoss is a powerful application server and there are many properties that can be set here.
Start JBoss on ‘server’: [JbossDIR]/bin/run.sh (run.bat on Windows)
Start your listener, the program auto-terminates in 2 minutes.
Run your sender.
You will get the following console output from the listener:
Incoming message: Billy Madison 1995
Incoming message: Happy Gilmore 1996
Incoming message: The Waterboy 1998
Incoming message: Bid Daddy 1999
Incoming message: Mr.Deeds 2002
Incoming message: Eight Crazy Nights 2002
Incoming message: Anger Management 2003
JMS Architecture for Redundancy
Using Java Message Service to Create Redundancy. Eliminating the database as the single point of failure.
The J2EE tutorial from Sun’s website is an excellent introduction to JMS.
http://java.sun.com/j2ee/1.4/docs/tutorial/doc/index.html
|
|
|
Copyright © 1999-2009 Outwardmotion