1 7 package org.jboss.tutorial.mdbstandalone; 8 9 import java.util.Hashtable ; 10 import javax.naming.InitialContext ; 11 import javax.jms.QueueConnection ; 12 import javax.jms.QueueSender ; 13 import javax.jms.QueueSession ; 14 import javax.jms.Queue ; 15 import javax.jms.QueueConnectionFactory ; 16 import javax.jms.TextMessage ; 17 import javax.jms.TopicConnection ; 18 import javax.jms.TopicPublisher ; 19 import javax.jms.TopicSession ; 20 import javax.jms.Topic ; 21 import javax.jms.TopicConnectionFactory ; 22 import org.jboss.ejb3.embedded.EJB3StandaloneBootstrap; 23 24 30 public class Main 31 { 32 public static void main(String [] args) throws Exception 33 { 34 EJB3StandaloneBootstrap.boot(null); 35 EJB3StandaloneBootstrap.deployXmlResource("jboss-jms-beans.xml"); 36 EJB3StandaloneBootstrap.deployXmlResource("testjms.xml"); 37 EJB3StandaloneBootstrap.deployXmlResource("ejb3-deployment.xml"); 38 39 executeQueue(); 40 executeTopic(); 41 42 EJB3StandaloneBootstrap.shutdown(); 43 44 System.out.println("takes a few seconds to shutdown because of JMS threads...This will be fixed in later releases"); 45 } 46 47 private static void executeQueue() 48 throws Exception 49 { 50 51 QueueConnection cnn = null; 52 QueueSender sender = null; 53 QueueSession session = null; 54 55 Queue queue = (Queue ) getInitialContext().lookup("queue/mdbtest"); 56 QueueConnectionFactory factory = (QueueConnectionFactory ) getInitialContext().lookup("java:/ConnectionFactory"); 57 cnn = factory.createQueueConnection(); 58 session = cnn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE); 59 60 TextMessage msg = session.createTextMessage("Hello World"); 61 62 sender = session.createSender(queue); 63 sender.send(msg); 64 65 Thread.sleep(1000); 66 session.close(); 67 cnn.close(); 68 } 69 70 private static void executeTopic() 71 throws Exception 72 { 73 74 TopicConnection cnn = null; 75 TopicPublisher sender = null; 76 TopicSession session = null; 77 78 Topic topic = (Topic ) getInitialContext().lookup("topic/topictest"); 79 TopicConnectionFactory factory = (TopicConnectionFactory ) getInitialContext().lookup("java:/ConnectionFactory"); 80 cnn = factory.createTopicConnection(); 81 session = cnn.createTopicSession(false, QueueSession.AUTO_ACKNOWLEDGE); 82 83 TextMessage msg = session.createTextMessage("Hello World"); 84 85 sender = session.createPublisher(topic); 86 sender.send(msg); 87 88 Thread.sleep(1000); 89 session.close(); 90 cnn.close(); 91 } 92 93 public static InitialContext getInitialContext() throws Exception 94 { 95 Hashtable props = getInitialContextProperties(); 96 return new InitialContext (props); 97 } 98 99 private static Hashtable getInitialContextProperties() 100 { 101 Hashtable props = new Hashtable (); 102 props.put("java.naming.factory.initial", "org.jnp.interfaces.LocalOnlyContextFactory"); 103 props.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces"); 104 return props; 105 } 106 } 107 | Popular Tags |