Bael 2560 spring jms error handler (#6447)

* Spring jms error handler

* BAEL-2560 Reverting a change
This commit is contained in:
Shubhra Srivastava 2019-03-04 08:06:11 +05:30 committed by maibin
parent 94969021ca
commit 00c115229b
5 changed files with 68 additions and 39 deletions

View File

@ -0,0 +1,17 @@
package com.baeldung.spring.jms;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ErrorHandler;
public class SampleJmsErrorHandler implements ErrorHandler {
private static final Logger LOG = LoggerFactory.getLogger(SampleJmsErrorHandler.class);
@Override
public void handleError(Throwable t) {
LOG.warn("In default jms error handler...");
LOG.error("Error Message : {}", t.getMessage());
}
}

View File

@ -26,4 +26,8 @@ public class SampleJmsMessageSender {
public void sendMessage(final Employee employee) {
this.jmsTemplate.convertAndSend(employee);
}
public void sendTextMessage(String msg) {
this.jmsTemplate.send(queue, s -> s.createTextMessage(msg));
}
}

View File

@ -27,6 +27,9 @@ public class SampleListener implements MessageListener {
try {
String msg = ((TextMessage) message).getText();
System.out.println("Received message: " + msg);
if (msg == null) {
throw new IllegalArgumentException("Null value received...");
}
} catch (JMSException ex) {
throw new RuntimeException(ex);
}
@ -37,4 +40,5 @@ public class SampleListener implements MessageListener {
Map map = (Map) this.jmsTemplate.receiveAndConvert();
return new Employee((String) map.get("name"), (Integer) map.get("age"));
}
}

View File

@ -1,51 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:amq="http://activemq.apache.org/schema/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:amq="http://activemq.apache.org/schema/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">
<!-- JmsTemplate Definition -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="defaultDestination" ref="destinationQueue" />
<property name="messageConverter" ref="myMessageConverter" />
</bean>
<bean id="amqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<constructor-arg index="0" value="tcp://localhost:61616" />
</bean>
<!-- JmsTemplate Definition -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="defaultDestination" ref="destinationQueue" />
<property name="messageConverter" ref="myMessageConverter" />
</bean>
<!-- ConnectionFactory Definition -->
<bean id="connectionFactory"
class="org.springframework.jms.connection.SingleConnectionFactory">
<constructor-arg ref="amqConnectionFactory" />
<bean id="amqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<constructor-arg index="0" value="tcp://localhost:61616" />
</bean>
</bean>
<!-- ConnectionFactory Definition -->
<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<constructor-arg ref="amqConnectionFactory" />
</bean>
<bean id="destinationQueue" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg index="0" value="IN_QUEUE" />
</bean>
<bean id="destinationQueue" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg index="0" value="IN_QUEUE" />
</bean>
<bean id="SampleJmsMessageSender" class="com.baeldung.spring.jms.SampleJmsMessageSender">
<property name="queue" ref="destinationQueue" />
<property name="jmsTemplate" ref="jmsTemplate" />
</bean>
<bean id="myMessageConverter" class="com.baeldung.spring.jms.SampleMessageConverter" />
<bean id="SampleJmsMessageSender" class="com.baeldung.spring.jms.SampleJmsMessageSender">
<property name="queue" ref="destinationQueue" />
<property name="jmsTemplate" ref="jmsTemplate" />
</bean>
<!-- this is the Message-Driven POJO (MDP) -->
<bean id="messageListener" class="com.baeldung.spring.jms.SampleListener">
<property name="jmsTemplate" ref="jmsTemplate" />
<property name="queue" ref="destinationQueue" />
</bean>
<bean id="myMessageConverter" class="com.baeldung.spring.jms.SampleMessageConverter" />
<bean id="errorHandler" class="com.baeldung.spring.jms.SampleJmsErrorHandler" />
<!-- this is the Message-Driven POJO (MDP) -->
<bean id="messageListener" class="com.baeldung.spring.jms.SampleListener">
<property name="jmsTemplate" ref="jmsTemplate" />
<property name="queue" ref="destinationQueue" />
</bean>
<!-- and this is the message listener container -->
<bean id="jmsContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destinationName" value="IN_QUEUE" />
<property name="messageListener" ref="messageListener" />
</bean>
<!-- and this is the message listener container -->
<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destinationName" value="IN_QUEUE" />
<property name="messageListener" ref="messageListener" />
<property name="errorHandler" ref="errorHandler" />
</bean>
</beans>

View File

@ -8,12 +8,14 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DefaultTextMessageSenderIntegrationTest {
private static SampleJmsMessageSender messageProducer;
private static SampleListener messageListener;
@SuppressWarnings("resource")
@BeforeClass
public static void setUp() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:EmbeddedActiveMQ.xml", "classpath:applicationContext.xml");
messageProducer = (SampleJmsMessageSender) applicationContext.getBean("SampleJmsMessageSender");
messageListener = (SampleListener) applicationContext.getBean("messageListener");
}
@Test
@ -21,4 +23,9 @@ public class DefaultTextMessageSenderIntegrationTest {
messageProducer.simpleSend();
}
@Test
public void testSendTextMessage() {
messageProducer.sendTextMessage(null);
}
}