KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > tutorial > mdbstandalone > Main


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.tutorial.mdbstandalone;
8
9 import java.util.Hashtable JavaDoc;
10 import javax.naming.InitialContext JavaDoc;
11 import javax.jms.QueueConnection JavaDoc;
12 import javax.jms.QueueSender JavaDoc;
13 import javax.jms.QueueSession JavaDoc;
14 import javax.jms.Queue JavaDoc;
15 import javax.jms.QueueConnectionFactory JavaDoc;
16 import javax.jms.TextMessage JavaDoc;
17 import javax.jms.TopicConnection JavaDoc;
18 import javax.jms.TopicPublisher JavaDoc;
19 import javax.jms.TopicSession JavaDoc;
20 import javax.jms.Topic JavaDoc;
21 import javax.jms.TopicConnectionFactory JavaDoc;
22 import org.jboss.ejb3.embedded.EJB3StandaloneBootstrap;
23
24 /**
25  * Comment
26  *
27  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
28  * @version $Revision: 1.1 $
29  */

30 public class Main
31 {
32    public static void main(String JavaDoc[] args) throws Exception JavaDoc
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 JavaDoc
49    {
50
51       QueueConnection JavaDoc cnn = null;
52       QueueSender JavaDoc sender = null;
53       QueueSession JavaDoc session = null;
54
55       Queue JavaDoc queue = (Queue JavaDoc) getInitialContext().lookup("queue/mdbtest");
56       QueueConnectionFactory JavaDoc factory = (QueueConnectionFactory JavaDoc) getInitialContext().lookup("java:/ConnectionFactory");
57       cnn = factory.createQueueConnection();
58       session = cnn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
59
60       TextMessage JavaDoc 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 JavaDoc
72    {
73
74       TopicConnection JavaDoc cnn = null;
75       TopicPublisher JavaDoc sender = null;
76       TopicSession JavaDoc session = null;
77
78       Topic JavaDoc topic = (Topic JavaDoc) getInitialContext().lookup("topic/topictest");
79       TopicConnectionFactory JavaDoc factory = (TopicConnectionFactory JavaDoc) getInitialContext().lookup("java:/ConnectionFactory");
80       cnn = factory.createTopicConnection();
81       session = cnn.createTopicSession(false, QueueSession.AUTO_ACKNOWLEDGE);
82
83       TextMessage JavaDoc 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 JavaDoc getInitialContext() throws Exception JavaDoc
94    {
95       Hashtable JavaDoc props = getInitialContextProperties();
96       return new InitialContext JavaDoc(props);
97    }
98
99    private static Hashtable JavaDoc getInitialContextProperties()
100    {
101       Hashtable JavaDoc props = new Hashtable JavaDoc();
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