activemq-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gregg <garl...@hotmail.com>
Subject Re: Same message redelivered to multiple consumers after failover
Date Wed, 12 Mar 2014 22:27:04 GMT
*Listener:*

package com.test;
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;

public class TestListener {
	public static void main(String[] args) {
		try {	
			ActiveMQConnectionFactory connectionFactory = new
ActiveMQConnectionFactory("tcp://localhost:61616");
			Connection connection = connectionFactory.createConnection();
			Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
			Destination destination = session.createQueue("TEST.QUEUE");
			MessageConsumer consumer = session.createConsumer(destination);
			consumer.setMessageListener(new AmqListener());
			connection.start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static class AmqListener implements MessageListener
	{
		@Override
		public void onMessage(Message message) {
			try	{
				TextMessage textMessage = (TextMessage) message;
				System.out.print("\nReceived the following message: " +
textMessage.getText());
				System.out.print(", Redelivery: " + message.getJMSRedelivered());
				System.out.print(", Count: " +
message.getLongProperty("JMSXDeliveryCount"));
			}
			catch (JMSException e) {
				e.printStackTrace();
			}
		}
	}

	public TestListener() {
		super();
	}
}

*Producer:*

package com.test;
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;

public class TestProducer {
	public static void main(String[] args) {
		try {
			thread(new HelloWorldProducer(), false);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
 
	public static class HelloWorldProducer implements Runnable {
		public void run() {
			try {
				ActiveMQConnectionFactory connectionFactory = new
ActiveMQConnectionFactory("tcp://localhost:61616");
				Connection connection = connectionFactory.createConnection();
				connection.start();
				Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
				Destination destination = session.createQueue("TEST.QUEUE");
				MessageProducer producer = session.createProducer(destination);

				String text = "Test MSG: " + Thread.currentThread().getName() + " : " +
this.hashCode();
				TextMessage message = session.createTextMessage(text);
				System.out.println("Sent message: " + text);
				producer.send(message);

				session.close();
				connection.close();
			}
			catch (Exception e) {
				e.printStackTrace();
			}
		}
		public HelloWorldProducer() {}
	}

	public static void thread(Runnable runnable, boolean daemon) {
		Thread brokerThread = new Thread(runnable);
		brokerThread.setDaemon(daemon);
		brokerThread.start();
	}
    
	public TestProducer() {
		super();
	}
}

Thanks!



gtully wrote
> Can you share your code?
> On 12 Mar 2014 17:40, "gregg" &lt;

> garlakg@

> &gt; wrote:





--
View this message in context: http://activemq.2283324.n4.nabble.com/Same-message-redelivered-to-multiple-consumers-after-failover-tp4678640p4678922.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.

Mime
View raw message