KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mail > Producer


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2003 - 2006 ScalAgent Distributed Technologies
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  *
20  * Initial developer(s): ScalAgent Distributed Technologies
21  * Contributor(s):
22  */

23 package mail;
24
25 import javax.jms.*;
26 import javax.naming.*;
27
28 /**
29  * Produces messages on the queue and on the topic.
30  */

31 public class Producer {
32   static Context ictx = null;
33
34   public static void main(String JavaDoc[] args) throws Exception JavaDoc {
35     ictx = new InitialContext();
36     Topic topic = (Topic) ictx.lookup("mailTopic");
37     ConnectionFactory cf = (ConnectionFactory) ictx.lookup("cf");
38     ictx.close();
39
40     Connection cnx = cf.createConnection();
41     Session sess = cnx.createSession(true, 0);
42     MessageProducer producer = sess.createProducer(topic);
43
44     System.out.println("Produces 5 messages on the MailTopic.");
45
46     for (int i=0; i<5; i++) {
47       TextMessage msg = sess.createTextMessage();
48
49       msg.setBooleanProperty("showProperties", true);
50       msg.setStringProperty("property1", "valeur#" + i);
51       msg.setIntProperty("property2", i);
52       msg.setText("Queue : Test number #" + i);
53       producer.send(msg);
54     }
55     sess.commit();
56
57     System.out.println("Messages sent.");
58
59     cnx.close();
60   }
61 }
62
Popular Tags