01: /**
02: * JOnAS: Java(TM) Open Application Server
03: * Copyright (C) 1999-2004 Bull S.A.
04: * Contact: jonas-team@objectweb.org
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation; either
09: * 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 9697 2006-10-09 09:51:18Z coqp $
26: * --------------------------------------------------------------------------
27: */package samplemdb;
28:
29: import javax.naming.Context;
30: import javax.naming.InitialContext;
31: import javax.naming.*;
32: import javax.jms.*;
33:
34: /**
35: *
36: */
37: public class MdbClient {
38:
39: static Context ictx = null;
40:
41: static TopicConnectionFactory tcf = null;
42:
43: static TopicPublisher tp = null;
44:
45: static Topic topic = null;
46:
47: // JNDI name of the Topic
48: static String topicName = "mdbTopic";
49:
50: // JNDI name of the default connection factory
51: static String conFactName = "JTCF";
52:
53: public static void main(String[] arg) {
54: // Get InitialContext
55: try {
56: ictx = new InitialContext();
57: // lookup the TopicConnectionFactory through its JNDI name
58: tcf = (TopicConnectionFactory) ictx.lookup(conFactName);
59: System.out.println("JMS client: tcf = " + tcf.toString());
60: // lookup the Topic through its JNDI name
61: topic = (Topic) ictx.lookup(topicName);
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,
73: Session.AUTO_ACKNOWLEDGE);
74: tp = session.createPublisher(topic);
75: } catch (Exception e) {
76: e.printStackTrace();
77: System.exit(2);
78: }
79:
80: // publish 10 messages to the topic
81: int nbmess = 10;
82: try {
83: TextMessage message;
84: for (int i = 0; i < nbmess; i++) {
85: message = session.createTextMessage();
86: message.setText("Message" + i);
87: tp.publish(message, DeliveryMode.PERSISTENT, 3, 0);
88: System.out.println("MdbClient: publish Message" + i);
89:
90: }
91: session.close();
92: tc.close();
93: } catch (Exception e) {
94: e.printStackTrace();
95: System.exit(2);
96: }
97: System.out.println("MDBsample is Ok");
98: }
99: }
|