Created
January 31, 2019 22:22
-
-
Save adelinor/0da3d9116ed3fad1606351a00b336280 to your computer and use it in GitHub Desktop.
Very simple consumption with MessageListenerContainer in a JUnit Test. This is inspired by https://www.logicbig.com/tutorials/spring-framework/spring-integration/jms-template-with-listener.html
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
package com.github.gist.adelinor.cameljmscalculator.messaging.config; | |
import static org.junit.Assert.assertFalse; | |
import static org.junit.Assert.assertNotNull; | |
import static org.junit.Assert.assertTrue; | |
import javax.jms.Message; | |
import org.junit.Ignore; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.boot.test.context.SpringBootTest; | |
import org.springframework.jms.core.JmsTemplate; | |
import org.springframework.test.context.junit4.SpringRunner; | |
import com.github.gist.adelinor.cameljmscalculator.messaging.config.ListenerConfig.QueueListener; | |
@RunWith(SpringRunner.class) | |
@SpringBootTest | |
public class ConsumerTest { | |
@Autowired | |
private QueueListener listener; | |
@Autowired | |
private JmsTemplate jmsTemplate; | |
@Ignore | |
@Test | |
public void testConsumer() { | |
assertNotNull(listener); | |
assertFalse(listener.getMessages().isEmpty()); | |
System.out.println(listener.getMessages().size()); | |
} | |
@Test | |
public void testSendAndReceive() { | |
assertNotNull(listener); | |
assertTrue(listener.getMessages().isEmpty()); | |
jmsTemplate.send("jms/calculator/inbox", s -> s.createTextMessage("12 + 13")); | |
Message message = null; | |
for (int i = 0; message == null && i < 50; i++) { | |
System.out.println(i + " :: " + listener.getMessages().size()); | |
waitABit(); | |
if (listener.getMessages().size() > 0) { | |
message = listener.getMessages().get(0); | |
} | |
} | |
} | |
private synchronized void waitABit() { | |
try { | |
wait(200); | |
} catch (InterruptedException exc) { | |
// TODO Auto-generated catch block | |
exc.printStackTrace(); | |
} | |
} | |
} |
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
package com.github.gist.adelinor.cameljmscalculator.messaging.config; | |
import java.util.ArrayList; | |
import java.util.List; | |
import javax.jms.ConnectionFactory; | |
import javax.jms.Message; | |
import javax.jms.MessageListener; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.jms.core.JmsTemplate; | |
import org.springframework.jms.listener.DefaultMessageListenerContainer; | |
import org.springframework.jms.listener.MessageListenerContainer; | |
import org.springframework.jms.support.destination.DestinationResolver; | |
@Configuration | |
public class ListenerConfig { | |
public class QueueListener implements MessageListener { | |
private List<Message> messages = new ArrayList<>(); | |
@Override | |
public void onMessage(Message message) { | |
messages.add(message); | |
} | |
public List<Message> getMessages() { | |
return messages; | |
} | |
} | |
@Bean | |
public QueueListener queueListener() { | |
return new QueueListener(); | |
} | |
@Bean | |
public MessageListenerContainer listenerContainer( | |
ConnectionFactory connectionFactory, | |
DestinationResolver destinationResolver, | |
QueueListener queueListener) { | |
DefaultMessageListenerContainer result = new DefaultMessageListenerContainer(); | |
result.setConnectionFactory(connectionFactory); | |
result.setDestinationResolver(destinationResolver); | |
result.setDestinationName("jms/calculator/outbox"); | |
result.setMessageListener(queueListener); | |
return result; | |
} | |
@Bean | |
public JmsTemplate jmsTemplate(ConnectionFactory connectionFactory, | |
DestinationResolver destinationResolver) { | |
JmsTemplate result = new JmsTemplate(connectionFactory); | |
result.setDestinationResolver(destinationResolver); | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment