转自:
In this example we shall show you how to create a simple hello World example in JMS, using JBoss 5.1. JMS is a very popular API standard for messaging, and most messaging systems provide a JMS API. To create a simple JMS example that produces and consumes a simple hello World message one should perform the following steps:
-
Create a JNDI from which to lookup our JMS objects. We are using theInitialContext(Hashtable,?> environment) constructor with a where we have set the properties needed for connecting to JBoss, such as the initial context factory to use, the service provider that is the localhost, on port 1099 and the package prefix to use when loading in the context factory.
-
Lookup the JMS connection factory from the JBoss 5.1 object store, usinglookup(String name) API method of .
-
Lookup a queue from the JBoss 5.1 object store, using lookup(String name) API method of Context.
-
Create a connection to the JBoss 5.1 Message Service, using createConnection() API method of javax.jms.ConnectionFactory.
-
Create a non transacted JMS Session, with AUTO_ACKNOWLEDGE acknowledge mode within the connection, usingcreateSession(boolean arg0, int arg1) API method of javax.jms.Connection.
-
Create a message producer to put messages on the queue, with createProducer(Destination arg0) API method ofjavax.jms.Session.
-
Create a message, with createTextMessage() API method of javax.jms.Session and setText(String arg0) API method ofjavax.jms.TextMessage and send it to the queue, with send(Message arg) method of javax.jms.MessageProducer.
-
Create a message consumer that will consume orders from the queue, with createConsumer(Destination arg0) API method ofjavax.jms.Session.
-
Make sure to start the connection, or delivery won’t occur on it. Use start() method of javax.jms.Connection.
-
Receive the message from the queue, using receive() API method of javax.jms.MessageConsumer and get the contents of the message, with getText() API method of javax.jms.TextMessage.
-
Close the session and connection resources, using close() API methods of javax.jms.Session and javax.jms.Connection.
Let’s take a look at the code snippet that follows:
Output:
Sending Message: Hello World
Read Message: Hello World
This was an example of how to create a simple JMS example using JBoss 5.1.
阅读(840) | 评论(0) | 转发(0) |