package se.callista.soalabs.loadtests; /** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.util.Date; import javax.jms.Connection; import javax.jms.Destination; import javax.jms.ExceptionListener; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageConsumer; import javax.jms.Session; import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; public class LoadTestJmsConsumer implements ExceptionListener { private String subject = "Soalabs.Loadtests.Out"; private String user = ActiveMQConnection.DEFAULT_USER; private String password = ActiveMQConnection.DEFAULT_PASSWORD; private String url = ActiveMQConnection.DEFAULT_BROKER_URL; private int waitTime = 5000; private Connection connection = null; private Session session = null; private Destination destination = null; private MessageConsumer consumer = null; public static void main(String[] args) { while (true) { LoadTestJmsConsumer c = new LoadTestJmsConsumer(); c.initJmsProducer(); long ts = System.currentTimeMillis(); c.receiveTestMessages(); System.out.println("Done in " + (System.currentTimeMillis() - ts) + " ms"); c.cleanupJmsProducer(); } // System.exit(0); } public LoadTestJmsConsumer() {} public void initJmsProducer() { System.out.println("Connecting to URL: " + url); System.out.println("Consuming queue(" + waitTime + "): " + subject); try { ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url); connection = connectionFactory.createConnection(); connection.setExceptionListener(this); connection.start(); session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); destination = session.createQueue(subject); consumer = session.createConsumer(destination); } catch (JMSException e) { throw new RuntimeException(e); } } public void cleanupJmsProducer() { System.out.println("Disconnecting..."); try { if (consumer != null) consumer.close(); if (session != null) session.close(); if (connection != null) { connection.stop(); connection.close(); } } catch (JMSException e) { throw new RuntimeException(e); } } public synchronized void onException(JMSException ex) { System.out.println("JMS Exception occured."); ex.printStackTrace(); } protected void receiveTestMessages() { long msgCount = 0; long tsMin = Long.MAX_VALUE; long tsMax = Long.MIN_VALUE; try { Message message; while ((message = consumer.receive(waitTime)) != null) { TextMessage msg = (TextMessage)message; long ts = msg.getJMSTimestamp(); msgCount++; if (ts < tsMin) tsMin = ts; if (ts > tsMax) tsMax = ts; // System.out.println("TS: " + new Date(ts) + ", ms: " + ts + ", length: " + msg.getText().length()); } } catch (JMSException e) { throw new RuntimeException(e); } System.out.println("MsgCnt: " + msgCount); if (msgCount > 0) { System.out.println("Min TS: [" + new Date(tsMin) + "], Max TS: [" + new Date(tsMax) + "], TS-diff: [" + (tsMax - tsMin) + "] ms"); } } }