Krishna iResearch Intelligent Cloud Platform - KingCobra - Byzantine request servicing software
 All Namespaces
Listener.java
1 /***************************************************************************************
2 KingCobra - A Research Software for Distributed Request Service on Cloud with Arbiters
3 
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 --------------------------------------------------------------------------------------------------
18 Copyright (C):
19 Srinivasan Kannan (alias) Ka.Shrinivaasan (alias) Shrinivas Kannan
20 Independent Open Source Developer, Researcher and Consultant
21 Ph: 9789346927, 9003082186, 9791165980
22 Open Source Products Profile(Krishna iResearch): http://sourceforge.net/users/ka_shrinivaasan, https://www.ohloh.net/accounts/ka_shrinivaasan
23 Personal website(research): https://sites.google.com/site/kuja27/
24 emails: ka.shrinivaasan@gmail.com, shrinivas.kannan@gmail.com, kashrinivaasan@live.com
25 --------------------------------------------------------------------------------------------------
26 *****************************************************************************************/
27 
28 
29 
30 
47 package kingcobra;
48 
49 import org.apache.qpid.amqp_1_0.jms.impl.*;
50 import javax.jms.*;
51 
52 class Listener {
53 
54  public static void main(String []args) throws JMSException {
55 
56  String user = env("ACTIVEMQ_USER", "admin");
57  String password = env("ACTIVEMQ_PASSWORD", "password");
58  String host = env("ACTIVEMQ_HOST", "localhost");
59  int port = Integer.parseInt(env("ACTIVEMQ_PORT", "5672"));
60  String destination = arg(args, 0, "queue://kingcobraq");
61 
62  ConnectionFactoryImpl factory = new ConnectionFactoryImpl(host, port, user, password);
63  Destination dest = null;
64  if( destination.startsWith("topic://") ) {
65  dest = new TopicImpl(destination);
66  } else {
67  dest = new QueueImpl(destination);
68  }
69 
70  Connection connection = factory.createConnection(user, password);
71  connection.start();
72  Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
73  MessageConsumer consumer = session.createConsumer(dest);
74  long start = System.currentTimeMillis();
75  long count = 1;
76  System.out.println("Waiting for messages...");
77  while(true) {
78  Message msg = consumer.receive();
79  if( msg instanceof TextMessage ) {
80  String body = ((TextMessage) msg).getText();
81  if( "SHUTDOWN".equals(body)) {
82  long diff = System.currentTimeMillis() - start;
83  System.out.println(String.format("Received %d in %.2f seconds", count, (1.0*diff/1000.0)));
84  connection.close();
85  System.exit(1);
86  } else {
87  try {
88  if( count != msg.getIntProperty("id") ) {
89  System.out.println("mismatch: "+count+"!="+msg.getIntProperty("id"));
90  }
91  } catch (NumberFormatException ignore) {
92  }
93  if( count == 1 ) {
94  start = System.currentTimeMillis();
95  } else if( count % 1000 == 0 ) {
96  System.out.println(String.format("Received %d messages.", count));
97  }
98  count ++;
99  }
100 
101  } else {
102  System.out.println("Unexpected message type: "+msg.getClass());
103  }
104  }
105  }
106 
107  private static String env(String key, String defaultValue) {
108  String rc = System.getenv(key);
109  if( rc== null )
110  return defaultValue;
111  return rc;
112  }
113 
114  private static String arg(String []args, int index, String defaultValue) {
115  if( index < args.length )
116  return args[index];
117  else
118  return defaultValue;
119  }
120 }