KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > simple > Producer


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 /**
19  * The SimpleQueueSender class consists only of a main method,
20  * which sends several messages to a queue.
21  *
22  * Run this program in conjunction with SimpleQueueReceiver.
23  * Specify a queue name on the command line when you run the
24  * program. By default, the program sends one message. Specify
25  * a number after the queue name to send that number of messages.
26  */

27 package org.apache.activemq.simple;
28
29 import javax.jms.Connection JavaDoc;
30 import javax.jms.ConnectionFactory JavaDoc;
31 import javax.jms.Destination JavaDoc;
32 import javax.jms.JMSException JavaDoc;
33 import javax.jms.MessageProducer JavaDoc;
34 import javax.jms.Session JavaDoc;
35 import javax.jms.TextMessage JavaDoc;
36
37 import org.apache.activemq.ActiveMQConnectionFactory;
38 import org.apache.activemq.command.ActiveMQQueue;
39
40 public class Producer {
41
42     private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
43             .getLog(Producer.class);
44     
45     public static void main(String JavaDoc[] args) throws JMSException JavaDoc, InterruptedException JavaDoc {
46
47         String JavaDoc url = "peer://localhost1/groupA?persistent=false";
48         if( args.length>0 ) {
49             url = args[0];
50         }
51
52         ConnectionFactory JavaDoc connectionFactory = new ActiveMQConnectionFactory(url);
53         Destination JavaDoc destination = new ActiveMQQueue("TEST.QUEUE");
54
55         Connection JavaDoc connection = connectionFactory.createConnection();
56         Session JavaDoc session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
57         MessageProducer JavaDoc producer = session.createProducer(destination);
58         TextMessage JavaDoc message = session.createTextMessage();
59         for (int i = 0; i < 1000; i++) {
60             message.setText("This is message " + (i + 1));
61             log.info("Sending message: " + message.getText());
62             producer.send(message);
63             Thread.sleep(1000);
64         }
65         connection.close();
66         
67     }
68 }
69
70 // END SNIPPET: demo
71
Popular Tags