0

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

vishva
  • 366
  • 5
  • 17

1 Answers1

0

I recommend against opening/closing connections/sessions/queues for every operation, but instead can use a pool to minimize how many of each resource is needed. Pretty sure that connections are thread-safe but that sessions are not and you need to create/use/dedicate a session per each active thread. By pooling, you can minimize the number of sessions created to however active threads are currently running and reuse them later.

So I'm assuming that you're having a resource problem, that even though it appears that everything's getting closed/released correctly, something isn't (perhaps outside the code I see here). Have you checked the activemq logs? Have you debugged through this and made sure that it's not hanging when trying to create the n'th connection or session?

Scott Sosna
  • 1,443
  • 1
  • 8
  • 8