KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > samplemdb > MdbClient


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer(s): ____________________________________.
22  * Contributor(s): ______________________________________.
23  *
24  * --------------------------------------------------------------------------
25  * $Id: MdbClient.java,v 1.7 2004/04/19 06:39:30 benoitf Exp $
26  * --------------------------------------------------------------------------
27  */

28
29 // MdbClient.java
30
// mini Client for accessing bean Mdb
31
package samplemdb;
32
33 import javax.naming.Context;
34 import javax.naming.InitialContext;
35 import javax.naming.*;
36 import javax.jms.*;
37
38 /**
39  *
40  */

41 public class MdbClient {
42
43     static Context ictx = null;
44
45     static TopicConnectionFactory tcf = null;
46
47     static TopicPublisher tp = null;
48
49     static Topic topic = null;
50
51     // JNDI name of the Topic
52
static String topicName = "mdbTopic";
53
54     // JNDI name of the default connection factory
55
static String conFactName = "JTCF";
56
57     public static void main(String[] arg) {
58         // Get InitialContext
59
try {
60             ictx = new InitialContext();
61             // lookup the TopicConnectionFactory through its JNDI name
62
tcf = (TopicConnectionFactory) ictx.lookup(conFactName);
63             System.out.println("JMS client: tcf = " + tcf.toString());
64             // lookup the Topic through its JNDI name
65
topic = (Topic) ictx.lookup(topicName);
66         } catch (NamingException e) {
67             e.printStackTrace();
68             System.exit(2);
69         }
70
71         TopicConnection tc = null;
72         TopicSession session = null;
73         try {
74             tc = tcf.createTopicConnection();
75             System.out.println("JMS client: tc = " + tc.toString());
76             session = tc.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
77             tp = session.createPublisher(topic);
78         } catch (Exception e) {
79             e.printStackTrace();
80             System.exit(2);
81         }
82
83         // publish 10 messages to the topic
84
int nbmess = 10;
85         try {
86             TextMessage message;
87             for (int i = 0; i < nbmess; i++) {
88                 message = session.createTextMessage();
89                 message.setText("Message" + i);
90                 tp.publish(message);
91
92             }
93             session.close();
94             /*
95              * temporary commented in order to avoid the blocking bug of the Sun
96              * JDK 1.3 on Linux tc.close();
97              */

98         } catch (Exception e) {
99             e.printStackTrace();
100             System.exit(2);
101         }
102         System.out.println("MDBsample is Ok");
103     }
104 }
Popular Tags