JBoss 5.1 中一个简单的 JMS 示例
| 01 | packagecom.javacodegeeks.snippets.enterprise; | 
| 02 | 
| 03 | importjava.util.Hashtable; | 
| 04 | 
| 05 | importjavax.jms.Connection; | 
| 06 | importjavax.jms.ConnectionFactory; | 
| 07 | importjavax.jms.Message; | 
| 08 | importjavax.jms.MessageConsumer; | 
| 09 | importjavax.jms.MessageProducer; | 
| 10 | importjavax.jms.Queue; | 
| 11 | importjavax.jms.Session; | 
| 12 | importjavax.jms.TextMessage; | 
| 13 | importjavax.naming.Context; | 
| 14 | importjavax.naming.InitialContext; | 
| 15 | 
| 16 | publicclassHelloWorldJMS { | 
| 17 | 
| 18 |     publicstaticvoidmain(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 = newHashtable(); | 
| 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 = newInitialContext(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 instanceofTextMessage) { | 
| 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) 收藏 举报
 
                    
                     
                    
                 
                    
                 
 
         
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号