I have used ActiveMQ as broker and JMS to receive async. messages in queue then start consuming these messages as soon as message comes in queue. For this i have Written producer and consumer code. Everything is working fine but the entire process is taking too much time around 2-3 minutes for 10000 records.(i have used a loop just for simulation) Below is the entire code:
This is JMS Producer:
public class JmsMessageProducer
{
public void jmsListener(String obj) throws Exception
{
BrokerService broker = BrokerFactory.createBroker(new URI("broker:(tcp://localhost:61616)"));
broker.start();
Connection connection = null;
Session session = null;
MessageProducer producer = null;
try
{
long millis = System.currentTimeMillis() % 1000;
// Producer
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
connection = connectionFactory.createConnection();
session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue("customerQueue3");
MessageConsumer consumer = session.createConsumer(queue);
consumer.setMessageListener(new ConsumerMessageListener("Consumer3"));
connection.start();
producer = session.createProducer(queue);
for(int i=0; i<10000; i++)
{
String payload = "Important Task"+i;
Message msg = session.createTextMessage(payload);
System.out.println("Sending text '" + payload + "'");
producer.send(msg);
}
long millis2 = System.currentTimeMillis() % 1000;
long millis3 = millis2- millis;
System.out.println("time taken: "+ millis3 );
}
finally
{
if(producer != null)
{
producer.close();
}
if (session != null)
{
session.close();
}
if (connection != null)
{
connection.close();
}
broker.stop();
}
}
}
This is listener code:
public class ConsumerMessageListener implements MessageListener
{
private String consumerName;
public ConsumerMessageListener(String consumerName)
{
this.consumerName = consumerName;
}
DummyAdapter adapter = new DummyAdapter();
public void onMessage(Message message)
{
TextMessage textMessage = (TextMessage) message;
try
{
System.out.println(consumerName + " received "+ textMessage.getText());
// adapter.dummy(textMessage.getText());
}
catch (JMSException e)
{
e.printStackTrace();
}
}
}
I am doing this first time. Can anyone tell me why is this process taking too much time? What am i doing wrong?