Krishna iResearch Intelligent Cloud Platform - KingCobra - Byzantine request servicing software
 All Namespaces
Publisher.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 Publisher {
53 
54  public static void main(String []args) throws Exception {
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  int messages = 10000;
63  int size = 256;
64 
65  String DATA = "abcdefghijklmnopqrstuvwxyz";
66  String body = "";
67  for( int i=0; i < size; i ++) {
68  body += DATA.charAt(i%DATA.length());
69  }
70 
71  ConnectionFactoryImpl factory = new ConnectionFactoryImpl(host, port, user, password);
72  Destination dest = null;
73  if( destination.startsWith("topic://") ) {
74  dest = new TopicImpl(destination);
75  } else {
76  dest = new QueueImpl(destination);
77  }
78 
79  Connection connection = factory.createConnection(user, password);
80  connection.start();
81  Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
82  MessageProducer producer = session.createProducer(dest);
83  producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
84 
85  for( int i=1; i <= messages; i ++) {
86  TextMessage msg = session.createTextMessage("#:"+i);
87  msg.setIntProperty("id", i);
88  producer.send(msg);
89  if( (i % 1000) == 0) {
90  System.out.println(String.format("Sent %d messages", i));
91  }
92  }
93 
94  producer.send(session.createTextMessage("SHUTDOWN"));
95  Thread.sleep(1000*3);
96  connection.close();
97  System.exit(0);
98  }
99 
100  private static String env(String key, String defaultValue) {
101  String rc = System.getenv(key);
102  if( rc== null )
103  return defaultValue;
104  return rc;
105  }
106 
107  private static String arg(String []args, int index, String defaultValue) {
108  if( index < args.length )
109  return args[index];
110  else
111  return defaultValue;
112  }
113 
114 }