I want to send acknowledgement to Activemq
when variable x
value is equals to 1
.If it not equals to 1
,I want to redeliver messages to Activemq
. Then only Activemq
delivers that messages again to subscribers.For this I written the following programs.
MessageConsumer.java :
public class Consumer extends HttpServlet {
@Override
protected void service(HttpServletRequest arg0, HttpServletResponse arg1)
throws ServletException, IOException {
try {
ActiveMQConnectionFactory connectionFactory=new ActiveMQConnectionFactory("admin","admin","tcp://localhost:61617");
RedeliveryPolicy policy = new RedeliveryPolicy();
policy.setInitialRedeliveryDelay(1000L);
policy.setMaximumRedeliveries(RedeliveryPolicy.NO_MAXIMUM_REDELIVERIES);
connectionFactory.setRedeliveryPolicy(policy);
connectionFactory.setUseRetroactiveConsumer(true);
Connection connection=connectionFactory.createConnection();
final Session session=connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
Topic queue=session.createTopic("MessageTesting");
javax.jms.MessageConsumer consumer=session.createConsumer(queue);
//anonymous class
MessageListener listener = new MessageListener() {
@Override
public void onMessage(Message msg) {
TextMessage msg1=(TextMessage)msg;
try {
String messageBody=msg1.getText();
if (x==1) {
//Process was completely done,so I am sending acknowledge
session.commit();
}
else {
//Process is not done sucessfully, So I want to redeliver messages, For this
session.rollback();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
};
consumer.setMessageListener(listener);
connection.start();
}
}
Is this correct way to do this.can you suggest me,Is there any efficient way.
Thanks.