Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package example;
- import org.apache.activemq.ActiveMQConnectionFactory;
- import org.apache.activemq.command.ActiveMQTopic;
- import javax.jms.*;
- class DurableSub {
- public static void main(String []args) throws JMSException {
- String user = env("ACTIVEMQ_USER", "admin");
- String password = env("ACTIVEMQ_PASSWORD", "password");
- String host = env("ACTIVEMQ_HOST", "localhost");
- int port = Integer.parseInt(env("ACTIVEMQ_PORT", "61616"));
- String destination = arg(args, 0, "event");
- ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://" + host + ":" + port);
- Connection connection = factory.createConnection(user, password);
- connection.setClientID("broker1.consumer1");
- connection.start();
- Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
- Topic dest = new ActiveMQTopic(destination);
- MessageConsumer consumer = session.createDurableSubscriber(dest,"SubDemo");
- long start = System.currentTimeMillis();
- long count = 1;
- System.out.println("Waiting for messages...");
- while(true) {
- Message msg = consumer.receive();
- if( msg instanceof TextMessage ) {
- String body = ((TextMessage) msg).getText();
- if( "SHUTDOWN".equals(body)) {
- long diff = System.currentTimeMillis() - start;
- System.out.println(String.format("Received %d in %.2f seconds", count, (1.0*diff/1000.0)));
- break;
- } else {
- if( count != msg.getIntProperty("id") ) {
- System.out.println("mismatch: "+count+"!="+msg.getIntProperty("id"));
- }
- count = msg.getIntProperty("id");
- if( count == 0 ) {
- start = System.currentTimeMillis();
- }
- if( count % 1000 == 0 ) {
- System.out.println(String.format("Received %d messages.", count));
- }
- count ++;
- }
- } else {
- System.out.println("Unexpected message type: "+msg.getClass());
- }
- }
- connection.close();
- }
- private static String env(String key, String defaultValue) {
- String rc = System.getenv(key);
- if( rc== null )
- return defaultValue;
- return rc;
- }
- private static String arg(String []args, int index, String defaultValue) {
- if( index < args.length )
- return args[index];
- else
- return defaultValue;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement