use of org.springframework.messaging.support.ChannelInterceptor in project spring-integration by spring-projects.
the class PollingConsumer method applyBeforeHandle.
private Message<?> applyBeforeHandle(Message<?> message, Deque<ExecutorChannelInterceptor> interceptorStack) {
Message<?> theMessage = message;
for (ChannelInterceptor interceptor : this.channelInterceptors) {
if (interceptor instanceof ExecutorChannelInterceptor) {
ExecutorChannelInterceptor executorInterceptor = (ExecutorChannelInterceptor) interceptor;
theMessage = executorInterceptor.beforeHandle(theMessage, this.inputChannel, this.handler);
if (message == null) {
if (logger.isDebugEnabled()) {
logger.debug(executorInterceptor.getClass().getSimpleName() + " returned null from beforeHandle, i.e. precluding the send.");
}
triggerAfterMessageHandled(null, null, interceptorStack);
return null;
}
interceptorStack.add(executorInterceptor);
}
}
return theMessage;
}
use of org.springframework.messaging.support.ChannelInterceptor in project spring-integration by spring-projects.
the class EnableIntegrationTests method testParentChildAnnotationConfigurationFromAnotherPackage.
@Test
public void testParentChildAnnotationConfigurationFromAnotherPackage() {
AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext();
child.register(org.springframework.integration.configuration2.ChildConfiguration.class);
child.setParent(this.context);
child.refresh();
AbstractMessageChannel foo = child.getBean("foo", AbstractMessageChannel.class);
ChannelInterceptor baz = child.getBean("baz", ChannelInterceptor.class);
assertTrue(foo.getChannelInterceptors().contains(baz));
assertFalse(this.output.getChannelInterceptors().contains(baz));
child.close();
}
use of org.springframework.messaging.support.ChannelInterceptor in project spring-integration by spring-projects.
the class AbstractMessageChannel method send.
/**
* Send a message on this channel. If the channel is at capacity, this
* method will block until either the timeout occurs or the sending thread
* is interrupted. If the specified timeout is 0, the method will return
* immediately. If less than zero, it will block indefinitely (see
* {@link #send(Message)}).
* @param message the Message to send
* @param timeout the timeout in milliseconds
* @return <code>true</code> if the message is sent successfully,
* <code>false</code> if the message cannot be sent within the allotted
* time or the sending thread is interrupted.
*/
@Override
public boolean send(Message<?> message, long timeout) {
Assert.notNull(message, "message must not be null");
Assert.notNull(message.getPayload(), "message payload must not be null");
if (this.shouldTrack) {
message = MessageHistory.write(message, this, this.getMessageBuilderFactory());
}
Deque<ChannelInterceptor> interceptorStack = null;
boolean sent = false;
boolean metricsProcessed = false;
MetricsContext metrics = null;
boolean countsEnabled = this.countsEnabled;
ChannelInterceptorList interceptors = this.interceptors;
AbstractMessageChannelMetrics channelMetrics = this.channelMetrics;
SampleFacade sample = null;
try {
if (this.datatypes.length > 0) {
message = this.convertPayloadIfNecessary(message);
}
boolean debugEnabled = this.loggingEnabled && logger.isDebugEnabled();
if (debugEnabled) {
logger.debug("preSend on channel '" + this + "', message: " + message);
}
if (interceptors.getSize() > 0) {
interceptorStack = new ArrayDeque<>();
message = interceptors.preSend(message, this, interceptorStack);
if (message == null) {
return false;
}
}
if (countsEnabled) {
metrics = channelMetrics.beforeSend();
if (this.metricsCaptor != null) {
sample = this.metricsCaptor.start();
}
sent = doSend(message, timeout);
if (sample != null) {
sample.stop(sendTimer(sent));
}
channelMetrics.afterSend(metrics, sent);
metricsProcessed = true;
} else {
sent = doSend(message, timeout);
}
if (debugEnabled) {
logger.debug("postSend (sent=" + sent + ") on channel '" + this + "', message: " + message);
}
if (interceptorStack != null) {
interceptors.postSend(message, this, sent);
interceptors.afterSendCompletion(message, this, sent, null, interceptorStack);
}
return sent;
} catch (Exception e) {
if (countsEnabled && !metricsProcessed) {
if (sample != null) {
sample.stop(buildSendTimer(false, e.getClass().getSimpleName()));
}
channelMetrics.afterSend(metrics, false);
}
if (interceptorStack != null) {
interceptors.afterSendCompletion(message, this, sent, e, interceptorStack);
}
throw IntegrationUtils.wrapInDeliveryExceptionIfNecessary(message, () -> "failed to send Message to channel '" + this.getComponentName() + "'", e);
}
}
use of org.springframework.messaging.support.ChannelInterceptor in project spring-integration by spring-projects.
the class GlobalChannelInterceptorTests method validateGlobalInterceptor.
@Test
public void validateGlobalInterceptor() throws Exception {
Map<String, ChannelInterceptorAware> channels = applicationContext.getBeansOfType(ChannelInterceptorAware.class);
for (String channelName : channels.keySet()) {
ChannelInterceptorAware channel = channels.get(channelName);
if (channelName.equals("nullChannel")) {
continue;
}
ChannelInterceptor[] interceptors = channel.getChannelInterceptors().toArray(new ChannelInterceptor[channel.getChannelInterceptors().size()]);
if (channelName.equals("inputA")) {
// 328741
assertTrue(interceptors.length == 10);
assertEquals("interceptor-three", interceptors[0].toString());
assertEquals("interceptor-two", interceptors[1].toString());
assertEquals("interceptor-eight", interceptors[2].toString());
assertEquals("interceptor-seven", interceptors[3].toString());
assertEquals("interceptor-five", interceptors[4].toString());
assertEquals("interceptor-six", interceptors[5].toString());
assertEquals("interceptor-ten", interceptors[6].toString());
assertEquals("interceptor-eleven", interceptors[7].toString());
assertEquals("interceptor-four", interceptors[8].toString());
assertEquals("interceptor-one", interceptors[9].toString());
} else if (channelName.equals("inputB")) {
assertTrue(interceptors.length == 6);
assertEquals("interceptor-three", interceptors[0].toString());
assertEquals("interceptor-two", interceptors[1].toString());
assertEquals("interceptor-ten", interceptors[2].toString());
assertEquals("interceptor-eleven", interceptors[3].toString());
assertEquals("interceptor-four", interceptors[4].toString());
assertEquals("interceptor-one", interceptors[5].toString());
} else if (channelName.equals("foo")) {
assertTrue(interceptors.length == 6);
assertEquals("interceptor-two", interceptors[0].toString());
assertEquals("interceptor-five", interceptors[1].toString());
assertEquals("interceptor-ten", interceptors[2].toString());
assertEquals("interceptor-eleven", interceptors[3].toString());
assertEquals("interceptor-four", interceptors[4].toString());
assertEquals("interceptor-one", interceptors[5].toString());
} else if (channelName.equals("bar")) {
assertTrue(interceptors.length == 4);
assertEquals("interceptor-eight", interceptors[0].toString());
assertEquals("interceptor-seven", interceptors[1].toString());
assertEquals("interceptor-ten", interceptors[2].toString());
assertEquals("interceptor-eleven", interceptors[3].toString());
} else if (channelName.equals("baz")) {
assertTrue(interceptors.length == 2);
assertEquals("interceptor-ten", interceptors[0].toString());
assertEquals("interceptor-eleven", interceptors[1].toString());
} else if (channelName.equals("inputWithProxy")) {
assertTrue(interceptors.length == 6);
} else if (channelName.equals("test")) {
assertNotNull(interceptors);
assertTrue(interceptors.length == 2);
List<String> interceptorNames = new ArrayList<String>();
for (ChannelInterceptor interceptor : interceptors) {
interceptorNames.add(interceptor.toString());
}
assertTrue(interceptorNames.contains("interceptor-ten"));
assertTrue(interceptorNames.contains("interceptor-eleven"));
}
}
}
use of org.springframework.messaging.support.ChannelInterceptor in project spring-integration by spring-projects.
the class ChannelInterceptorTests method testInterceptorBeanWithPNamespace.
@Test
public void testInterceptorBeanWithPNamespace() {
ConfigurableApplicationContext ac = new ClassPathXmlApplicationContext("ChannelInterceptorTests-context.xml", ChannelInterceptorTests.class);
ChannelInterceptorAware channel = ac.getBean("input", AbstractMessageChannel.class);
List<ChannelInterceptor> interceptors = channel.getChannelInterceptors();
ChannelInterceptor channelInterceptor = interceptors.get(0);
assertThat(channelInterceptor, Matchers.instanceOf(PreSendReturnsMessageInterceptor.class));
String foo = ((PreSendReturnsMessageInterceptor) channelInterceptor).getFoo();
assertTrue(StringUtils.hasText(foo));
assertEquals("foo", foo);
ac.close();
}
Aggregations