Getting Connection object when using JMSListener

Options
vshivk99
vshivk99 Member Posts: 13

Hi,
Is there any way to get the handle of Connection object when using @jmslistener?
@Bean(name = "FeeBillConnectionFactory")
public JmsListenerContainerFactory jmsListenerContainerFactory(ConnectionFactory cachingConnectionFactory){
DefaultJmsListenerContainerFactory jmsListenerContainerFactory = new DefaultJmsListenerContainerFactory();
jmsListenerContainerFactory.setConnectionFactory(cachingConnectionFactory);
jmsListenerContainerFactory.setConcurrency("2-10");
jmsListenerContainerFactory.setSessionTransacted(true);
jmsListenerContainerFactory.setSessionAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE);
return jmsListenerContainerFactory;
}

  @JmsListener(destination = "security", containerFactory="FeeBillConnectionFactory") //, concurrency = "15-20")
  public void handle(Message message, Session session) throws InterruptedException, JMSException {
        System.out.println("handle()"+message.getJMSMessageID());

        if (message instanceof TextMessage) {
              TextMessage tm = (TextMessage) message;
              try {                     

                    TestFunction tf=new TestFunction(message, session);    
                    Thread t = new Thread(tf);
                    t.setDaemon(true);
                    t.start();


                    }

               catch (Exception e) {
                    e.printStackTrace();
              }
        } else {
              System.out.println("In Spring Reciever"+message.toString());
        }

Comments

  • marc
    marc Member, Administrator, Moderator, Employee Posts: 920 admin
    Options

    Hi @vshivk99,
    Tbh I'm not sure off the top of my head as I don't see an obvious way, but I would be curious what you'd want to do with the Connection object? Usually the connections are managed by the ConnectionFactory itself in a Spring JMS App and from a dev perspective you can access the ConnectionFactory or the Session object (as you've shown in your code above)

  • vshivk99
    vshivk99 Member Posts: 13
    edited May 2021 #3
    Options

    Hi @marc ,
    We are trying to create SolaceConnector common library. As part of it for sending , i wanted to create a new session in sender so that we do not get the issue of Commit issue. For creating new session need Connection object.
    final Session sessionn = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

    The approach I am taking is, as soon as message comes I will create a new thread and with the thread i will process all the request and complete the operation and if it is R/R then will also send the reply back. For sending the reply back, I will use the Sender code.

    Thanks and Regards,
    Shiva

  • marc
    marc Member, Administrator, Moderator, Employee Posts: 920 admin
    Options

    Hi @vshivk99,

    Unless I'm missing something I would take a closer look at using the Spring JMS DefaultMessageListenerContainer and see if it meets your needs before creating your own library to do this. They have a pretty good write-up at the top of that page. Using that DMLC Spring will already allow you to specify an exact number of concurrent consumers using setConcurrentConsumers or event a range using concurrency and each consumer will process messages in its own thread.

  • vshivk99
    vshivk99 Member Posts: 13
    Options

    Hi @marc , I will share the code with you tomorrow which we are using.