pretty_pretty_fish

导航

JBoss 5.1 中一个简单的 JMS 示例

01 package com.javacodegeeks.snippets.enterprise;
02  
03 import java.util.Hashtable;
04  
05 import javax.jms.Connection;
06 import javax.jms.ConnectionFactory;
07 import javax.jms.Message;
08 import javax.jms.MessageConsumer;
09 import javax.jms.MessageProducer;
10 import javax.jms.Queue;
11 import javax.jms.Session;
12 import javax.jms.TextMessage;
13 import javax.naming.Context;
14 import javax.naming.InitialContext;
15  
16 public class HelloWorldJMS {
17  
18     public static void main(String[] args) {
19         try {
20  
21             /*
22              * Connecting to JBoss naming service running on local host and on
23              * default port 1099 the environment that should be created is like the
24              * one shown below :
25              */
26             Hashtable env = new Hashtable();
27             env.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
28             env.put(Context.PROVIDER_URL, "jnp://localhost:1099");
29             env.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
30              
31             // Create the initial context
32             Context ctx = new InitialContext(env);
33              
34             // Lookup the JMS connection factory from the JBoss 5.1 object store
35             ConnectionFactory connectionFactory = (ConnectionFactory) ctx.lookup("ConnectionFactory");
36    
37             // Lookup a queue from the JBoss 5.1 object store
38             Queue queue = (javax.jms.Queue)ctx.lookup("/queue/DLQ");
39  
40             // Create a connection to the JBoss 5.1 Message Service
41             Connection connection = connectionFactory.createConnection();
42  
43             // Create a session within the connection
44             Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
45  
46             // Create a message producer to put messages on the queue
47             MessageProducer messageProducer = session.createProducer(queue);
48  
49             //Create and send a message to the queue
50             TextMessage textMessage = session.createTextMessage();
51             textMessage.setText("Hello World");
52             System.out.println("Sending Message: " + textMessage.getText());
53             messageProducer.send(textMessage);
54  
55             // Create a message consumer
56             MessageConsumer messageConsumer = session.createConsumer(queue);
57  
58             // Start the Connection created
59             connection.start();
60  
61             // Receive a message from the queue.
62             Message msg = messageConsumer.receive();
63  
64             // Retrieve the contents of the message.
65             if (msg instanceof TextMessage) {
66                 TextMessage txtMsg = (TextMessage) msg;
67                 System.out.println("Read Message: " + txtMsg.getText());
68             }
69  
70             //Close the session and connection resources.
71             session.close();
72             connection.close();
73  
74         catch (Exception ex) {
75             ex.printStackTrace();
76         }
77     }
78  
79 }

posted on 2013-03-08 23:47  pretty_pretty_fish  阅读(146)  评论(0)    收藏  举报