KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > newsamplemdb > MdbClient


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 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.1 2004/05/04 14:37:23 fmaistre Exp $
26  * --------------------------------------------------------------------------
27  */

28
29 // MdbClient.java
30
// mini Client for accessing bean Mdb
31

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

94         } catch (Exception e) {
95             e.printStackTrace();
96             System.exit(2);
97         }
98         System.out.println("MDBsample is Ok");
99     }
100 }
101
Popular Tags