2

Here are my Hornetq configuration in spring boot.

spring.hornetq.mode=embedded 
spring.hornetq.embedded.enabled=true
spring.hornetq.embedded.persistent=true 
spring.hornetq.port=5445 
spring.hornetq.embedded.queues=jms.testqueue

Here is my Producer

public class Producer {@Inject
private JmsTemplate jmsTemplate;
public void resolveError( String message) {  
   try{
        jmsTemplate.convertAndSend(DATA_QUEUE, message);
        }catch(Exception e){
        //log error
        }    
}}

Here is my Consumer

 @JmsListener(destination = DATA_QUEUE)
public void consume(String message) throws InterruptedException {
    log.info("Receiving  event: {}", message);
    try {
       //do stuff with message
    }catch (Exception e){
        log.error(e.toString());
    }
}

Here is my config file

@Configuration@EnableJms public class JmsConfig {
public static final String LOGGING_SCRAPPER_KEY ="DATA_SYNC_ERROR";
public static final String DATA_QUEUE = "jms.testqueue"; }

I want to slow down the consuming process of @JMSlistener, I don't want to the JMS listener hit the queue all the time any help is appreciated, thanks!!

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
NagaRajendra
  • 159
  • 3
  • 16
  • 1
    What is it that you exactly want? What do you mean with *I want to slow down the consuming process of @JMSlistener*? – M. Deinum Jan 12 '16 at 06:53
  • If JMSlistener is consuming per second i want the listener to consume for every 5 sec – NagaRajendra Jan 12 '16 at 14:43
  • Then just configure that... See http://docs.spring.io/spring/docs/current/spring-framework-reference/html/jms.html#jms-annotated-support how to configure JMS. Just set the receive timeout to the value you want. – M. Deinum Jan 12 '16 at 14:47
  • Thanks, Deinum set timeout did not work for me in my scenario but I am able to use jmsTemplate.setDelivaryDelay – NagaRajendra Jan 13 '16 at 18:26

2 Answers2

3

The listeners that are created under the covers for each @JmsListener annotated method are held in a registry as explained in the documentation

If you want to pause your listener, it is very easy to look it up and stop it. Let's assume you have a way to invoke the following bean (JMX endpoint, secure rest mapping, whatever):

static class YourService {

    private final JmsListenerEndpointRegistry registry;

    @Autowired
    public YourService(JmsListenerEndpointRegistry registry) {
        this.registry = registry;
    }

    public void stopListener() {
        this.registry.getListenerContainer("myListener").stop();
    }

    public void startListener() {
        this.registry.getListenerContainer("myListener").start();
    }

}

Then you need to associate the proper id to your listener (myListener) in the example above.

@JmsListener(id = "myListener", destination = DATA_QUEUE)
public void consume(String message) throws InterruptedException { ... }
Stephane Nicoll
  • 31,977
  • 9
  • 97
  • 89
  • Do i need to configure the JmsListenerEndpointRegistry in my JMSconfig file ? how do i give a name to my listener – NagaRajendra Jan 12 '16 at 14:48
  • understood given id="myListener" will register my jmslistener to the JMSendpointregistery right? – NagaRajendra Jan 12 '16 at 15:15
  • is there a way to do this, If JMSlistener is consuming per second i want the listener to consume for every 5 sec – NagaRajendra Jan 12 '16 at 15:51
  • HI [Stephane Nicoll](http://stackoverflow.com/users/613628/st%C3%A9phane-nicoll) how should i initialize the "registry" using constructor, where will get the JmsListenerEndpointRegistry object to pass to the constructor, please help [Stephane Nicoll](http://stackoverflow.com/users/613628/st%C3%A9phane-nicoll) – NagaRajendra Jan 13 '16 at 17:26
0

I'm not able to set the consuming time of JmsListener but I found an alternative where I'm able to set delivery delay time limit on jmsTemplate instead, use jmsTemplate setDeliveryDelay which will delay sending it to the queue. Either way, it is delayed only if you go with delaying the consuming process of JMS listener you will have the message in the queue in my approach it won't be in the queue until the delay delivery time.

NagaRajendra
  • 159
  • 3
  • 16