INT-1855 fixed support for applying channel interceptors on PollableJmsChannel

This commit is contained in:
Oleg Zhurakousky
2011-04-11 16:38:49 -04:00
parent 7713ee4764
commit 75aa5b3058
2 changed files with 92 additions and 2 deletions

View File

@@ -23,6 +23,7 @@ import org.springframework.jms.core.JmsTemplate;
/**
* @author Mark Fisher
* @author Oleg Zhurakousky
* @since 2.0
*/
public class PollableJmsChannel extends AbstractJmsChannel implements PollableChannel {
@@ -33,14 +34,22 @@ public class PollableJmsChannel extends AbstractJmsChannel implements PollableCh
public Message<?> receive() {
if (!this.getInterceptors().preReceive(this)) {
return null;
}
Object object = this.getJmsTemplate().receiveAndConvert();
if (object == null) {
return null;
}
Message<?> replyMessage = null;
if (object instanceof Message<?>) {
return (Message<?>) object;
replyMessage = (Message<?>) object;
}
return MessageBuilder.withPayload(object).build();
else {
replyMessage = MessageBuilder.withPayload(object).build();
}
return this.getInterceptors().postReceive(replyMessage, this) ;
}
public Message<?> receive(long timeout) {

View File

@@ -16,17 +16,27 @@
package org.springframework.integration.jms;
import static junit.framework.Assert.assertNull;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import java.util.ArrayList;
import java.util.List;
import javax.jms.Destination;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.channel.ChannelInterceptor;
import org.springframework.integration.jms.config.ActiveMqTestUtils;
import org.springframework.integration.jms.config.JmsChannelFactoryBean;
import org.springframework.integration.message.GenericMessage;
@@ -87,5 +97,76 @@ public class PollableJmsChannelTests {
assertNotNull(result2);
assertEquals("bar", result2.getPayload());
}
@Test
public void queueNameWithFalsePreReceiveInterceptors() throws Exception {
ActiveMqTestUtils.prepare();
this.connectionFactory = new ActiveMQConnectionFactory();
this.connectionFactory.setBrokerURL("vm://localhost");
JmsChannelFactoryBean factoryBean = new JmsChannelFactoryBean(false);
factoryBean.setConnectionFactory(this.connectionFactory);
factoryBean.setDestinationName("someDynamicQueue");
factoryBean.setPubSubDomain(false);
List<ChannelInterceptor> interceptorList = new ArrayList<ChannelInterceptor>();
ChannelInterceptor interceptor = spy(new SampleInterceptor(false));
interceptorList.add(interceptor);
factoryBean.setInterceptors(interceptorList);
factoryBean.afterPropertiesSet();
PollableJmsChannel channel = (PollableJmsChannel) factoryBean.getObject();
boolean sent1 = channel.send(new GenericMessage<String>("foo"));
assertTrue(sent1);
Message<?> result1 = channel.receive(10000);
assertNull(result1);
verify(interceptor, times(1)).preReceive(Mockito.any(MessageChannel.class));
verify(interceptor, times(0)).postReceive(Mockito.any(Message.class), Mockito.any(MessageChannel.class));
}
@Test
public void queueNameWithTruePreReceiveInterceptors() throws Exception {
ActiveMqTestUtils.prepare();
this.connectionFactory = new ActiveMQConnectionFactory();
this.connectionFactory.setBrokerURL("vm://localhost");
JmsChannelFactoryBean factoryBean = new JmsChannelFactoryBean(false);
factoryBean.setConnectionFactory(this.connectionFactory);
factoryBean.setDestinationName("someDynamicQueue");
factoryBean.setPubSubDomain(false);
List<ChannelInterceptor> interceptorList = new ArrayList<ChannelInterceptor>();
ChannelInterceptor interceptor = spy(new SampleInterceptor(true));
interceptorList.add(interceptor);
factoryBean.setInterceptors(interceptorList);
factoryBean.afterPropertiesSet();
PollableJmsChannel channel = (PollableJmsChannel) factoryBean.getObject();
boolean sent1 = channel.send(new GenericMessage<String>("foo"));
assertTrue(sent1);
Message<?> result1 = channel.receive(10000);
assertNotNull(result1);
verify(interceptor, times(1)).preReceive(Mockito.any(MessageChannel.class));
verify(interceptor, times(1)).postReceive(Mockito.any(Message.class), Mockito.any(MessageChannel.class));
}
public static class SampleInterceptor implements ChannelInterceptor {
private final boolean preRecieveFlag;
public SampleInterceptor(boolean preRecieveFlag){
this.preRecieveFlag = preRecieveFlag;
}
public Message<?> preSend(Message<?> message, MessageChannel channel) {
return message;
}
public void postSend(Message<?> message, MessageChannel channel,
boolean sent) {
}
public boolean preReceive(MessageChannel channel) {
return this.preRecieveFlag;
}
public Message<?> postReceive(Message<?> message, MessageChannel channel) {
return message;
}
}
}