I wrote a sample code to add elements to activemq, and then retrieve them. I was successfully able to add around 1000 elements, but while retrieving the elements, somehow code gets stuck after retrieving around 50 - 200 elements, even when the queue has a lot of elements.
Following is the code i used for adding elements to the queue
@POST
@Path("/addelementtoqueue")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public void addElementToQeueue(@FormParam("count") int count) throws Exception {
IntStream.range(0, count)
.forEach(e -> {
try {
addElement(e);
}catch(Exception e1) {
throw new RuntimeException(e1);
}
});
}
private void addElement(int i) throws Exception {
Connection conn = GlobalConfiguration.getJMSConnectionFactory().createConnection();
conn.start();
Session session = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
MessageProducer prod = session.createProducer(queue);
prod.send(queue, session.createTextMessage("message "+ i), DeliveryMode.PERSISTENT, 4, 0);
prod.close();
session.close();
conn.close();
}
and this is the snippet i am using for retrieving elements from the queue
@POST
@Path("/removeelementfromqueue")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public void removeElementToQeueue(@FormParam("count") int count) throws Exception {
IntStream.range(0, count)
.forEach(e -> {
try {
extractElement();
}catch(Exception e1) {
throw new RuntimeException(e1);
}
});
}
private void extractElement() throws Exception {
Connection conn = GlobalConfiguration.getJMSConnectionFactory().createConnection();
conn.start();
Session session = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
queue = session.createQueue("walkin.testing");
MessageConsumer consumer = session.createConsumer(queue);
TextMessage msg = (TextMessage)consumer.receive();
System.out.println(msg.getText());
msg.acknowledge();
consumer.close();
session.close();
conn.close();
}
I am getting the connection factory via resource.xml, the snippet for the same is
<resources>
<Resource id="MyJmsResourceAdapter" type="ActiveMQResourceAdapter">
BrokerXmlConfig = jdbcBroker:(tcp://0.0.0.0:61616)
ServerUrl = tcp://0.0.0.0:61616?jms.prefetchPolicy.queuePrefetch=0
</Resource>
<Resource id="MyJmsConnectionFactory" type="javax.jms.ConnectionFactory">
ResourceAdapter = MyJmsResourceAdapter
</Resource></resources>
I am using activeMQ 5.13.1, with apache-tomee-plus-1.7.2 and Java 8, jdbc store as mysql. I have configured activemq-jdbc-performance.xml as the configuration file for apache activemq.
I have tried to a lot of research on this one, but i am unable to identify the root cause of this issue. It would be highly helpful, if any one can suggest me what i am doing wrong