Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2298465
  • 博文数量: 321
  • 博客积分: 3440
  • 博客等级: 中校
  • 技术积分: 2992
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-24 09:08
个人简介

我就在这里

文章分类

全部博文(321)

文章存档

2015年(9)

2014年(84)

2013年(101)

2012年(25)

2011年(29)

2010年(21)

2009年(6)

2008年(23)

2007年(23)

分类: Java

2014-05-24 22:22:40

转自:

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:



  1. package com.javacodegeeks.snippets.enterprise;

  2. import java.util.Hashtable;

  3. import javax.jms.Connection;
  4. import javax.jms.ConnectionFactory;
  5. import javax.jms.Message;
  6. import javax.jms.MessageConsumer;
  7. import javax.jms.MessageProducer;
  8. import javax.jms.Queue;
  9. import javax.jms.Session;
  10. import javax.jms.TextMessage;
  11. import javax.naming.Context;
  12. import javax.naming.InitialContext;

  13. public class HelloWorldJMS {

  14.     public static void main(String[] args) {

  15.   try {



  16. /*


  17.  * Connecting to JBoss naming service running on local host and on


  18.  * default port 1099 the environment that should be created is like the


  19.  * one shown below :


  20.  */


  21. Hashtable env = new Hashtable();


  22. env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");


  23. env.put(Context.PROVIDER_URL, "jnp://localhost:1099");


  24. env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");





  25. // Create the initial context


  26. Context ctx = new InitialContext(env);





  27. // Lookup the JMS connection factory from the JBoss 5.1 object store


  28. ConnectionFactory connectionFactory = (ConnectionFactory) ctx.lookup("ConnectionFactory");
  29.   


  30. // Lookup a queue from the JBoss 5.1 object store


  31. Queue queue = (javax.jms.Queue)ctx.lookup("/queue/DLQ");



  32. // Create a connection to the JBoss 5.1 Message Service


  33. Connection connection = connectionFactory.createConnection();



  34. // Create a session within the connection


  35. Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);



  36. // Create a message producer to put messages on the queue


  37. MessageProducer messageProducer = session.createProducer(queue);



  38. //Create and send a message to the queue


  39. TextMessage textMessage = session.createTextMessage();


  40. textMessage.setText("Hello World");


  41. System.out.println("Sending Message: " + textMessage.getText());


  42. messageProducer.send(textMessage);



  43. // Create a message consumer


  44. MessageConsumer messageConsumer = session.createConsumer(queue);



  45. // Start the Connection created


  46. connection.start();



  47. // Receive a message from the queue.


  48. Message msg = messageConsumer.receive();



  49. // Retrieve the contents of the message.


  50. if (msg instanceof TextMessage) {


  51.     TextMessage txtMsg = (TextMessage) msg;


  52.     System.out.println("Read Message: " + txtMsg.getText());


  53. }



  54. //Close the session and connection resources.


  55. session.close();


  56. connection.close();


  57.   } catch (Exception ex) {


  58. ex.printStackTrace();

  59.   }
  60.     }

  61. }

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.


阅读(800) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~