Delta Tecnologia – WYSIWYG Rotating Header Image

JmsTemplate – Delayed message delivery

Procurando por um modo de utilizar o Scheduler do ActiveMQ junto ao Spring-JMS, nao encontramos muita informacao na net sobre isso, apenas este post, porem ele abstrai a funcao de Scheduler do servidor de JMS, deixando esta tarefa para o Java.

Como esta solucao nao eh exatamente oque estamos procurano, elaborei a seguinte implementacao para utilizar o Scheduler do ActiveMQ:

1
2
3
4
5
6
7
8
9
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext( "applicationContext.xml" );

JmsTemplate template = (JmsTemplate) ctx.getBean( "jmsTemplate" );
ActiveMQDestination destination = (ActiveMQDestination) ctx.getBean( "destinationFox" );

// sending a message
template.setDeliveryMode( DeliveryMode.PERSISTENT );
template.setPriority( 1 );
template.convertAndSend( destination, "Hi-Man", new ScheduleMessagePostProcessor( 10000l ) );
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package br.com.delta.testeJms.schedule;

import javax.jms.JMSException;
import javax.jms.Message;

import org.apache.activemq.ScheduledMessage;
import org.springframework.jms.core.MessagePostProcessor;

public class ScheduleMessagePostProcessor implements MessagePostProcessor {

private long delay = 0l;

public ScheduleMessagePostProcessor(long delay) {
this.delay = delay;
}

public Message postProcessMessage( Message message ) throws JMSException {

if (delay > 0) {
message.setLongProperty( ScheduledMessage.AMQ_SCHEDULED_DELAY, delay );
}

return message;
}

public void setDelay( long delay ) {

this.delay = delay;
}

}

Nao devem esquecer de ativar o Scheduler no ActiveMQ, modificando o broker no xml de configuracao:

1
<broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost" dataDirectory="${activemq.data}" schedulerSupport="true">

Eh isso ae pessoal, ate a proxima!

Abs.
Danilo

One Comment

  1. Arianne says:

    I read a lot of interesting content here. Probably you spend a lot
    of time writing, i know how to save you a lot of work,
    there is an online tool that creates unique, SEO friendly posts in seconds,
    just type in google – laranitas free content

Leave a Reply

Your email address will not be published. Required fields are marked *


*

Clef two-factor authentication