Created
March 14, 2018 09:33
-
-
Save MatthewDavidCampbell/11e1804bf10f73c717830ce86e04c6ed to your computer and use it in GitHub Desktop.
Example of JmsListenerEndpointRegistry and JmsTemplate
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 sdc.ip.framework.spring.messaging; | |
import io.vertx.junit5.VertxExtension; | |
import io.vertx.junit5.VertxTestContext; | |
import org.apache.activemq.ActiveMQConnectionFactory; | |
import org.junit.jupiter.api.Disabled; | |
import org.junit.jupiter.api.Test; | |
import org.junit.jupiter.api.extension.ExtendWith; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.context.ApplicationContext; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.jms.config.DefaultJmsListenerContainerFactory; | |
import org.springframework.jms.config.JmsListenerEndpointRegistry; | |
import org.springframework.jms.config.SimpleJmsListenerEndpoint; | |
import org.springframework.jms.connection.CachingConnectionFactory; | |
import org.springframework.jms.core.JmsTemplate; | |
import org.springframework.jms.support.converter.MessageConverter; | |
import org.springframework.jms.support.converter.SimpleMessageConverter; | |
import org.springframework.test.context.ContextConfiguration; | |
import org.springframework.test.context.junit.jupiter.SpringExtension; | |
import javax.jms.JMSException; | |
import javax.jms.Message; | |
import java.util.concurrent.TimeUnit; | |
import static org.junit.jupiter.api.Assertions.*; | |
@ExtendWith({SpringExtension.class, VertxExtension.class}) | |
@ContextConfiguration(classes = ActiveMqInSpringTest.JmsConfig.class ) | |
public class ActiveMqInSpringTest { | |
@Autowired | |
JmsTemplate jmsTemplate; | |
@Autowired | |
DefaultJmsListenerContainerFactory jmsListenerContainerFactory; | |
@Autowired | |
ApplicationContext applicationContext; | |
/** | |
* Basic example of an embedded ActiveMq with | |
* Vertx's test context | |
* | |
* @param testContext | |
*/ | |
@Test | |
public void basic(VertxTestContext testContext) { | |
// Given: Queue (destination) and message | |
final String destination = "basic"; | |
final String messageText = "Hello World!"; | |
// When: | |
jmsTemplate.convertAndSend(destination, messageText); | |
Message message = jmsTemplate.receive(destination); | |
// Then: | |
MessageConverter converter = new SimpleMessageConverter(); | |
try { | |
String text = (String) converter.fromMessage(message); | |
testContext.verify(() -> { | |
assertEquals(messageText, text); | |
testContext.completeNow(); | |
}); | |
} catch (JMSException e) { | |
testContext.failNow(e); | |
} | |
} | |
/** | |
* Not working | |
* | |
* @param testContext | |
*/ | |
@Test | |
@Disabled | |
public void register(VertxTestContext testContext) { | |
// Given: Queue (destination), message and a magical register | |
final JmsListenerEndpointRegistry endpointRegistry = new JmsListenerEndpointRegistry(); | |
final String destination = "register"; | |
final String messageText = "Hello World!"; | |
// When: Endpoint registered then send | |
SimpleJmsListenerEndpoint endpoint = new SimpleJmsListenerEndpoint(); | |
endpoint.setId("register"); | |
endpoint.setDestination(destination); | |
endpoint.setMessageListener(m -> { | |
testContext.completeNow(); | |
}); | |
endpointRegistry.registerListenerContainer(endpoint, jmsListenerContainerFactory); | |
endpointRegistry.setApplicationContext(applicationContext); | |
endpointRegistry.start(); | |
// Then: Test context triggered | |
try { | |
assertTrue(testContext.awaitCompletion(5, TimeUnit.SECONDS)); | |
} catch (InterruptedException e) { | |
assertFalse(true); | |
} finally { | |
endpointRegistry.stop(); | |
} | |
} | |
public static class JmsConfig { | |
@Bean | |
public ActiveMQConnectionFactory activeMQConnectionFactory() { | |
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(); | |
activeMQConnectionFactory.setBrokerURL("vm://embedded?broker.persistent=false,useShutdownHook=false"); | |
return activeMQConnectionFactory; | |
} | |
@Bean | |
public CachingConnectionFactory cachingConnectionFactory() { | |
return new CachingConnectionFactory(activeMQConnectionFactory()); | |
} | |
@Bean | |
public JmsTemplate jmsTemplate() { | |
return new JmsTemplate(cachingConnectionFactory()); | |
} | |
@Bean | |
public DefaultJmsListenerContainerFactory jmsListenerContainerFactory(JmsTemplate jmsTemplate) { | |
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory(); | |
factory.setConnectionFactory(jmsTemplate.getConnectionFactory()); | |
return factory; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment