KickJava   Java API By Example, From Geeks To Geeks.

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


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

28 package org.apache.activemq.simple;
29
30 import javax.jms.Connection JavaDoc;
31 import javax.jms.ConnectionFactory JavaDoc;
32 import javax.jms.Destination JavaDoc;
33 import javax.jms.JMSException JavaDoc;
34 import javax.jms.Message JavaDoc;
35 import javax.jms.MessageConsumer JavaDoc;
36 import javax.jms.Session JavaDoc;
37
38 import org.apache.activemq.ActiveMQConnectionFactory;
39 import org.apache.activemq.command.ActiveMQQueue;
40
41 public class Consumer {
42     
43     public static void main(String JavaDoc[] args) throws JMSException JavaDoc, InterruptedException JavaDoc {
44
45         String JavaDoc url = "tcp://localhost:61616";
46         if( args.length>0 ) {
47             url = args[0];
48         }
49         
50         ConnectionFactory JavaDoc connectionFactory = new ActiveMQConnectionFactory(url);
51         Destination JavaDoc destination = new ActiveMQQueue("TEST.QUEUE");
52
53         Connection JavaDoc connection = connectionFactory.createConnection();
54         connection.start();
55         
56         Session JavaDoc session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
57         MessageConsumer JavaDoc consumer = session.createConsumer(destination);
58         
59         for( ;; ) {
60             System.out.println("Waiting for message.");
61             Message JavaDoc message = consumer.receive();
62             if( message == null ) {
63                 break;
64             }
65             System.out.println("Got message: " + message);
66         }
67         
68         connection.close();
69     }
70 }
71
72 // END SNIPPET: demo
73
Popular Tags