KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > eventdumper > EventDumper


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.eventdumper;
17
18 import org.apache.xmlbeans.XmlObject;
19
20 import javax.jms.*;
21 import javax.naming.Context JavaDoc;
22 import javax.naming.InitialContext JavaDoc;
23 import java.util.Hashtable JavaDoc;
24 import java.io.StringReader JavaDoc;
25
26 /**
27  * Simple tool to dump all daisy JMS events to standard out. All parameters are hardcoded for now.
28  */

29 public class EventDumper {
30     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
31         new EventDumper().run();
32     }
33
34     public void run() throws Exception JavaDoc {
35         String JavaDoc jmsUserName = "admin";
36         String JavaDoc jmsPassword = "jmsadmin";
37
38         Hashtable JavaDoc environment = new Hashtable JavaDoc();
39         environment.put("java.naming.factory.initial", "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
40         environment.put("java.naming.provider.url", "tcp://localhost:61616");
41         Context JavaDoc context = new InitialContext JavaDoc(environment);
42
43         TopicConnectionFactory jmsFactory = (TopicConnectionFactory) context.lookup("ConnectionFactory");
44         Topic jmsTopic = (Topic) context.lookup("dynamicTopics/daisy");
45
46         TopicConnection jmsConnection = jmsFactory.createTopicConnection(jmsUserName, jmsPassword);
47
48         TopicSession jmsSession = jmsConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
49         TopicSubscriber jmsSubscriber = jmsSession.createSubscriber(jmsTopic);
50         jmsSubscriber.setMessageListener(new EventListener());
51
52         jmsConnection.start();
53
54         System.out.println("Started");
55
56     }
57
58     class EventListener implements MessageListener {
59         public void onMessage(Message aMessage) {
60             try {
61                 TextMessage message = (TextMessage)aMessage;
62                 String JavaDoc messageType = message.getStringProperty("type");
63                 System.out.println("================================= New Message ====================================");
64                 System.out.println("Received message of type: " + messageType);
65                 System.out.println("--------------------------------- Message Body -----------------------------------");
66                 // parse and print xml, this will ensure it is 'pretty printed'
67
XmlObject xml = XmlObject.Factory.parse(new StringReader JavaDoc(message.getText()));
68                 System.out.println(xml);
69             } catch (Exception JavaDoc e) {
70                 System.out.println("Exception processing message: " + e.toString());
71                 e.printStackTrace();
72             }
73         }
74     }
75 }
76
Popular Tags