Created
January 6, 2012 08:33
-
-
Save sfalquier/1569709 to your computer and use it in GitHub Desktop.
SimpleMessageConsumer to receive a simple String from a queue using ApacheMQ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.jms.Connection; | |
import javax.jms.Destination; | |
import javax.jms.JMSException; | |
import javax.jms.Message; | |
import javax.jms.MessageListener; | |
import javax.jms.Session; | |
import org.apache.activemq.ActiveMQConnectionFactory; | |
import org.springframework.jms.core.JmsTemplate; | |
import org.springframework.jms.listener.SimpleMessageListenerContainer; | |
public class SimpleMessageConsumer { | |
/* | |
* (non-Javadoc) | |
* | |
* @see com.wikal.agents.core.AgentIfc#Start(java.util.ArrayList) | |
*/ | |
public void start() { | |
SimpleMessageListenerContainer listener = new SimpleMessageListenerContainer(); | |
try { | |
// configure and starts connection to AMQ | |
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory( | |
"tcp://localhost:61616"); | |
JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory); | |
Connection connection; | |
connection = connectionFactory.createConnection(); | |
connection.start(); | |
Session session = connection.createSession(false, | |
Session.AUTO_ACKNOWLEDGE); | |
final Destination queue = jmsTemplate.getDestinationResolver() | |
.resolveDestinationName(session, "FOO.TEST", false); | |
// configure and starts listener | |
listener.setConnectionFactory(connectionFactory); | |
listener.setDestination(queue); | |
listener.setConcurrentConsumers(1); | |
listener.setMessageListener(new SimpleMessageListener()); | |
listener.start(); | |
System.out.println("Listener started on tcp://localhost:61616 [" | |
+ queue + "]"); | |
} catch (JMSException e) { | |
e.printStackTrace(); | |
} | |
} | |
public static void main(String args[]) { | |
new SimpleMessageConsumer().start(); | |
} | |
public class SimpleMessageListener implements MessageListener { | |
@Override | |
public void onMessage(Message msg) { | |
System.out.println("Received message = " + msg); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment