本文整理汇总了Java中org.springframework.messaging.support.ChannelInterceptorAdapter类的典型用法代码示例。如果您正苦于以下问题:Java ChannelInterceptorAdapter类的具体用法?Java ChannelInterceptorAdapter怎么用?Java ChannelInterceptorAdapter使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ChannelInterceptorAdapter类属于org.springframework.messaging.support包,在下文中一共展示了ChannelInterceptorAdapter类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testSimple
import org.springframework.messaging.support.ChannelInterceptorAdapter; //导入依赖的package包/类
@Test
public void testSimple() {
SpringIntegrationChannelBinder binder = createBinder();
DefaultPollableMessageSource pollableSource = new DefaultPollableMessageSource();
pollableSource.addInterceptor(new ChannelInterceptorAdapter() {
@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
return MessageBuilder.withPayload(((String) message.getPayload()).toUpperCase())
.copyHeaders(message.getHeaders())
.build();
}
});
ExtendedConsumerProperties<Object> properties = new ExtendedConsumerProperties<>(null);
properties.setMaxAttempts(2);
properties.setBackOffInitialInterval(0);
binder.bindPollableConsumer("foo", "bar", pollableSource, properties);
final AtomicInteger count = new AtomicInteger();
assertThat(pollableSource.poll(received -> {
assertThat(received.getPayload()).isEqualTo("POLLED DATA");
assertThat(received.getHeaders().get(MessageHeaders.CONTENT_TYPE)).isEqualTo("text/plain");
if (count.incrementAndGet() == 1) {
throw new RuntimeException("test retry");
}
})).isTrue();
assertThat(count.get()).isEqualTo(2);
}
示例2: testRoutingKeyExpression
import org.springframework.messaging.support.ChannelInterceptorAdapter; //导入依赖的package包/类
@Test
public void testRoutingKeyExpression() throws Exception {
RabbitTestBinder binder = getBinder();
ExtendedProducerProperties<RabbitProducerProperties> producerProperties = createProducerProperties();
producerProperties.getExtension().setRoutingKeyExpression("payload.field");
DirectChannel output = createBindableChannel("output", createProducerBindingProperties(producerProperties));
output.setBeanName("rkeProducer");
Binding<MessageChannel> producerBinding = binder.bindProducer("rke", output, producerProperties);
RabbitAdmin admin = new RabbitAdmin(this.rabbitAvailableRule.getResource());
Queue queue = new AnonymousQueue();
TopicExchange exchange = new TopicExchange("rke");
org.springframework.amqp.core.Binding binding = BindingBuilder.bind(queue).to(exchange).with("rkeTest");
admin.declareQueue(queue);
admin.declareBinding(binding);
output.addInterceptor(new ChannelInterceptorAdapter() {
@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
assertThat(message.getHeaders().get(RabbitExpressionEvaluatingInterceptor.ROUTING_KEY_HEADER))
.isEqualTo("rkeTest");
return message;
}
});
output.send(new GenericMessage<>(new Pojo("rkeTest")));
Object out = spyOn(queue.getName()).receive(false);
assertThat(out).isInstanceOf(byte[].class);
assertThat(new String((byte[]) out, StandardCharsets.UTF_8)).isEqualTo("{\"field\":\"rkeTest\"}");
producerBinding.unbind();
}
示例3: testEmbedded
import org.springframework.messaging.support.ChannelInterceptorAdapter; //导入依赖的package包/类
@Test
public void testEmbedded() {
SpringIntegrationChannelBinder binder = createBinder();
binder.setMessageSourceDelegate(() -> {
MessageValues original = new MessageValues("foo".getBytes(),
Collections.singletonMap(MessageHeaders.CONTENT_TYPE, "application/octet-stream"));
byte[] payload = new byte[0];
try {
payload = EmbeddedHeaderUtils.embedHeaders(original, MessageHeaders.CONTENT_TYPE);
}
catch (Exception e) {
fail(e.getMessage());
}
return new GenericMessage<>(payload);
});
ExtendedConsumerProperties<Object> properties = new ExtendedConsumerProperties<>(null);
properties.setHeaderMode(HeaderMode.embeddedHeaders);
DefaultPollableMessageSource pollableSource = new DefaultPollableMessageSource();
pollableSource.addInterceptor(new ChannelInterceptorAdapter() {
@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
return MessageBuilder.withPayload(new String((byte[]) message.getPayload()).toUpperCase())
.copyHeaders(message.getHeaders())
.build();
}
});
binder.bindPollableConsumer("foo", "bar", pollableSource, properties);
assertThat(pollableSource.poll(received -> {
assertThat(received.getPayload()).isEqualTo("FOO");
assertThat(received.getHeaders().get(MessageHeaders.CONTENT_TYPE)).isEqualTo("application/octet-stream");
})).isTrue();
}
示例4: testErrors
import org.springframework.messaging.support.ChannelInterceptorAdapter; //导入依赖的package包/类
@Test
public void testErrors() {
SpringIntegrationChannelBinder binder = createBinder();
DefaultPollableMessageSource pollableSource = new DefaultPollableMessageSource();
pollableSource.addInterceptor(new ChannelInterceptorAdapter() {
@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
return MessageBuilder.withPayload(((String) message.getPayload()).toUpperCase())
.copyHeaders(message.getHeaders())
.build();
}
});
ExtendedConsumerProperties<Object> properties = new ExtendedConsumerProperties<>(null);
properties.setMaxAttempts(2);
properties.setBackOffInitialInterval(0);
binder.bindPollableConsumer("foo", "bar", pollableSource, properties);
final CountDownLatch latch = new CountDownLatch(1);
this.context.getBean(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME, SubscribableChannel.class).subscribe(m -> {
latch.countDown();
});
final AtomicInteger count = new AtomicInteger();
assertThat(pollableSource.poll(received -> {
count.incrementAndGet();
throw new RuntimeException("test recoverer");
})).isTrue();
assertThat(count.get()).isEqualTo(2);
Message<?> lastError = binder.getLastError();
assertThat(lastError).isNotNull();
assertThat(((Exception) lastError.getPayload()).getCause().getMessage()).isEqualTo("test recoverer");
}
示例5: testErrorsNoRetry
import org.springframework.messaging.support.ChannelInterceptorAdapter; //导入依赖的package包/类
@Test
public void testErrorsNoRetry() {
SpringIntegrationChannelBinder binder = createBinder();
DefaultPollableMessageSource pollableSource = new DefaultPollableMessageSource();
pollableSource.addInterceptor(new ChannelInterceptorAdapter() {
@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
return MessageBuilder.withPayload(((String) message.getPayload()).toUpperCase())
.copyHeaders(message.getHeaders())
.build();
}
});
ExtendedConsumerProperties<Object> properties = new ExtendedConsumerProperties<>(null);
properties.setMaxAttempts(1);
binder.bindPollableConsumer("foo", "bar", pollableSource, properties);
final CountDownLatch latch = new CountDownLatch(1);
this.context.getBean(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME, SubscribableChannel.class).subscribe(m -> {
latch.countDown();
});
final AtomicInteger count = new AtomicInteger();
assertThat(pollableSource.poll(received -> {
count.incrementAndGet();
throw new RuntimeException("test recoverer");
})).isTrue();
assertThat(count.get()).isEqualTo(1);
}
示例6: interceptor
import org.springframework.messaging.support.ChannelInterceptorAdapter; //导入依赖的package包/类
private ChannelInterceptor interceptor() {
return new ChannelInterceptorAdapter() {
@Override
public void postSend(Message<?> message, MessageChannel channel,
boolean sent) {
OutboundMessageHandlerConfiguration.this.message = message;
OutboundMessageHandlerConfiguration.this.latch.countDown();
}
};
}
示例7: sessionContextChannelInterceptorAdapter
import org.springframework.messaging.support.ChannelInterceptorAdapter; //导入依赖的package包/类
@Bean
public ChannelInterceptorAdapter sessionContextChannelInterceptorAdapter() {
return new ChannelInterceptorAdapter() {
@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
StompHeaderAccessor accessor = StompHeaderAccessor.wrap(message);
StompCommand command = accessor.getCommand();
if (log.isDebugEnabled() && command != null) {
log.debug("StompCommand: " + command.toString());
}
String authToken = accessor.getFirstNativeHeader(ServerConstants.X_AUTH_TOKEN);
if (log.isDebugEnabled() && StringUtils.isNotEmpty(authToken)) {
log.debug("Header auth token: " + authToken);
}
if (StringUtils.isNotBlank(authToken)) {
// set cached authenticated user back in the spring security context
Authentication authentication = preAuthAuthenticationManager.authenticate(new PreAuthenticatedAuthenticationToken(authToken, "N/A"));
if (log.isDebugEnabled()) {
log.debug("Adding Authentication to SecurityContext for WebSocket call: " + authentication);
}
SpringSecurityHelper.setAuthentication(authentication);
}
return super.preSend(message, channel);
}
};
}
示例8: testRoutingKeyExpressionPartitionedAndDelay
import org.springframework.messaging.support.ChannelInterceptorAdapter; //导入依赖的package包/类
@Test
public void testRoutingKeyExpressionPartitionedAndDelay() throws Exception {
RabbitTestBinder binder = getBinder();
ExtendedProducerProperties<RabbitProducerProperties> producerProperties = createProducerProperties();
producerProperties.getExtension().setRoutingKeyExpression("payload.field");
// requires delayed message exchange plugin; tested locally
// producerProperties.getExtension().setDelayedExchange(true);
producerProperties.getExtension().setDelayExpression("1000");
producerProperties.setPartitionKeyExpression(new ValueExpression<>(0));
DirectChannel output = createBindableChannel("output", createProducerBindingProperties(producerProperties));
output.setBeanName("rkeProducer");
Binding<MessageChannel> producerBinding = binder.bindProducer("rkep", output, producerProperties);
RabbitAdmin admin = new RabbitAdmin(this.rabbitAvailableRule.getResource());
Queue queue = new AnonymousQueue();
TopicExchange exchange = new TopicExchange("rkep");
org.springframework.amqp.core.Binding binding =
BindingBuilder.bind(queue).to(exchange).with("rkepTest-0");
admin.declareQueue(queue);
admin.declareBinding(binding);
output.addInterceptor(new ChannelInterceptorAdapter() {
@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
assertThat(message.getHeaders().get(RabbitExpressionEvaluatingInterceptor.ROUTING_KEY_HEADER))
.isEqualTo("rkepTest");
assertThat(message.getHeaders().get(RabbitExpressionEvaluatingInterceptor.DELAY_HEADER))
.isEqualTo(1000);
return message;
}
});
output.send(new GenericMessage<>(new Pojo("rkepTest")));
Object out = spyOn(queue.getName()).receive(false);
assertThat(out).isInstanceOf(byte[].class);
assertThat(new String((byte[]) out, StandardCharsets.UTF_8)).isEqualTo("{\"field\":\"rkepTest\"}");
producerBinding.unbind();
}
示例9: configureClientInboundChannel
import org.springframework.messaging.support.ChannelInterceptorAdapter; //导入依赖的package包/类
@Override
public void configureClientInboundChannel(ChannelRegistration registration) {
registration.setInterceptors(new ChannelInterceptorAdapter() {
});
}