@Test public void testGatewayParser() throws Exception { setUp("JobLaunchingGatewayParserTests-context.xml", getClass()); final AbstractMessageChannel inputChannel = TestUtils.getPropertyValue(this.consumer, "inputChannel", AbstractMessageChannel.class); assertEquals("requestChannel", inputChannel.getComponentName()); final JobLaunchingMessageHandler jobLaunchingMessageHandler = TestUtils.getPropertyValue(this.consumer, "handler.jobLaunchingMessageHandler", JobLaunchingMessageHandler.class); assertNotNull(jobLaunchingMessageHandler); final MessagingTemplate messagingTemplate = TestUtils.getPropertyValue(this.consumer, "handler.messagingTemplate", MessagingTemplate.class); final Long sendTimeout = TestUtils.getPropertyValue(messagingTemplate, "sendTimeout", Long.class); assertEquals("Wrong sendTimeout", Long.valueOf(123L), sendTimeout); assertFalse(this.consumer.isRunning()); }
@Override public void addInterceptor(ChannelInterceptor interceptor) { super.addInterceptor(interceptor); if (interceptor instanceof ExecutorChannelInterceptor) { this.executorInterceptorsSize++; } }
/** * Send a message on this channel. If the channel is at capacity, this * method will block until either space becomes available or the sending * thread is interrupted. * @param message the Message to send * @return <code>true</code> if the message is sent successfully or * <code>false</code> if the sending thread is interrupted. */ @Override public boolean send(Message<?> message) { return this.send(message, -1); }
@Override protected C doGet() { this.channel.setDatatypes(this.datatypes.toArray(new Class<?>[this.datatypes.size()])); this.channel.setBeanName(getId()); this.channel.setInterceptors(this.interceptors); this.channel.setMessageConverter(this.messageConverter); return this.channel; }
/** * Returns the fully qualified channel name including the application context * id, if available. * * @return The name. */ public String getFullChannelName() { if (this.fullChannelName == null) { String contextId = getApplicationContextId(); String componentName = getComponentName(); componentName = (StringUtils.hasText(contextId) ? contextId + "." : "") + (StringUtils.hasText(componentName) ? componentName : "unknown.channel.name"); this.fullChannelName = componentName; } return this.fullChannelName; }
private Message<?> convertPayloadIfNecessary(Message<?> message) { // first pass checks if the payload type already matches any of the datatypes for (Class<?> datatype : this.datatypes) { if (datatype.isAssignableFrom(message.getPayload().getClass())) { return message; } } if (this.messageConverter != null) { // second pass applies conversion if possible, attempting datatypes in order for (Class<?> datatype : this.datatypes) { Object converted = this.messageConverter.fromMessage(message, datatype); if (converted != null) { if (converted instanceof Message) { return (Message<?>) converted; } else { return getMessageBuilderFactory() .withPayload(converted) .copyHeaders(message.getHeaders()) .build(); } } } } throw new MessageDeliveryException(message, "Channel '" + this.getComponentName() + "' expected one of the following datataypes [" + StringUtils.arrayToCommaDelimitedString(this.datatypes) + "], but received [" + message.getPayload().getClass() + "]"); }
Message<?> message = messageArg; if (this.shouldTrack) { message = MessageHistory.write(message, this, this.getMessageBuilderFactory()); try { if (this.datatypes.length > 0) { message = this.convertPayloadIfNecessary(message); sample = this.metricsCaptor.start(); sent = doSend(message, timeout); if (sample != null) { sample.stop(sendTimer(sent)); sent = doSend(message, timeout); if (countsAreEnabled && !metricsProcessed) { if (sample != null) { sample.stop(buildSendTimer(false, e.getClass().getSimpleName())); () -> "failed to send Message to channel '" + this.getComponentName() + "'", e);
@SuppressWarnings({ "rawtypes", "unchecked" }) private void addMatchingWireTaps(AbstractMessageChannel channel) { Assert.notNull(channel.getComponentName(), "channel name must not be null"); Set<Entry> patternWireTapEntries = this.channelPatternMappings.entrySet(); for (Entry patternWireTapEntry : patternWireTapEntries) { if (((Pattern) patternWireTapEntry.getKey()).matcher(channel.getComponentName()).matches()) { channel.addInterceptor((EsperWireTap) patternWireTapEntry.getValue()); } } } }
@Test public void afterCompletionWithSendException() { final AbstractMessageChannel testChannel = new AbstractMessageChannel() { @Override protected boolean doSend(Message<?> message, long timeout) { throw new RuntimeException("Simulated exception"); } }; AfterCompletionTestInterceptor interceptor1 = new AfterCompletionTestInterceptor(); AfterCompletionTestInterceptor interceptor2 = new AfterCompletionTestInterceptor(); testChannel.addInterceptor(interceptor1); testChannel.addInterceptor(interceptor2); try { testChannel.send(MessageBuilder.withPayload("test").build()); } catch (Exception ex) { assertEquals("Simulated exception", ex.getCause().getMessage()); } assertTrue(interceptor1.wasAfterCompletionInvoked()); assertTrue(interceptor2.wasAfterCompletionInvoked()); }
private void moveChannelInterceptors(AbstractMessageChannel existingMessageChannel, AbstractMessageChannel actualMessageChannel) { for (ChannelInterceptor channelInterceptor : existingMessageChannel.getChannelInterceptors()) { actualMessageChannel.addInterceptor(channelInterceptor); existingMessageChannel.removeInterceptor(channelInterceptor); } }
@Test public void testElapsed() { int sampleSize = 2; this.delay.configureMetrics(new AggregatingMessageChannelMetrics("foo", sampleSize)); this.delay.setStatsEnabled(true); this.delayer.configureMetrics(new AggregatingMessageHandlerMetrics("bar", sampleSize)); this.delayer.setStatsEnabled(true); GenericMessage<String> message = new GenericMessage<String>("foo"); int count = 4; for (int i = 0; i < count; i++) { this.delay.send(message); } assertEquals(count, this.delay.getSendCount()); assertEquals(count / sampleSize, this.delay.getSendDuration().getCount()); assertThat((int) this.delay.getMeanSendDuration() / sampleSize, greaterThanOrEqualTo(50)); assertEquals(count, this.delayer.getHandleCount()); assertEquals(count / sampleSize, this.delayer.getDuration().getCount()); assertThat((int) this.delayer.getMeanDuration() / sampleSize, greaterThanOrEqualTo(50)); }
@Override protected void onInit() { super.onInit(); if (!this.initialized && this.rabbitTemplate != null) { if (this.connectionFactory != null) { this.connectionFactory.addConnectionListener(this); } } this.initialized = true; }
@Override public void setInterceptors(List<ChannelInterceptor> interceptors) { super.setInterceptors(interceptors); for (ChannelInterceptor interceptor : interceptors) { if (interceptor instanceof ExecutorChannelInterceptor) { this.executorInterceptorsSize++; } } }
/** * 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. */ public final boolean send(Message<?> message, long timeout) { message = this.interceptors.preSend(message, this); if (message == null) { return false; } boolean sent = this.doSend(message, timeout); this.interceptors.postSend(message, this, sent); return sent; }
@Override protected C doGet() { this.channel.setDatatypes(this.datatypes.toArray(new Class<?>[this.datatypes.size()])); this.channel.setBeanName(getId()); this.channel.setInterceptors(this.interceptors); this.channel.setMessageConverter(this.messageConverter); return this.channel; }
Message<?> message = messageArg; if (this.shouldTrack) { message = MessageHistory.write(message, this, this.getMessageBuilderFactory()); try { if (this.datatypes.length > 0) { message = this.convertPayloadIfNecessary(message); sample = this.metricsCaptor.start(); sent = doSend(message, timeout); if (sample != null) { sample.stop(sendTimer(sent)); sent = doSend(message, timeout); if (countsAreEnabled && !metricsProcessed) { if (sample != null) { sample.stop(buildSendTimer(false, e.getClass().getSimpleName())); () -> "failed to send Message to channel '" + this.getComponentName() + "'", e);
/** * Returns the fully qualified channel name including the application context * id, if available. * * @return The name. */ public String getFullChannelName() { if (this.fullChannelName == null) { String contextId = getApplicationContextId(); String componentName = getComponentName(); componentName = (StringUtils.hasText(contextId) ? contextId + "." : "") + (StringUtils.hasText(componentName) ? componentName : "unknown.channel.name"); this.fullChannelName = componentName; } return this.fullChannelName; }
private Message<?> convertPayloadIfNecessary(Message<?> message) { // first pass checks if the payload type already matches any of the datatypes for (Class<?> datatype : this.datatypes) { if (datatype.isAssignableFrom(message.getPayload().getClass())) { return message; } } if (this.messageConverter != null) { // second pass applies conversion if possible, attempting datatypes in order for (Class<?> datatype : this.datatypes) { Object converted = this.messageConverter.fromMessage(message, datatype); if (converted != null) { if (converted instanceof Message) { return (Message<?>) converted; } else { return getMessageBuilderFactory() .withPayload(converted) .copyHeaders(message.getHeaders()) .build(); } } } } throw new MessageDeliveryException(message, "Channel '" + this.getComponentName() + "' expected one of the following datataypes [" + StringUtils.arrayToCommaDelimitedString(this.datatypes) + "], but received [" + message.getPayload().getClass() + "]"); }
@Override protected void onInit() { super.onInit(); if (!this.initialized && this.rabbitTemplate != null) { if (this.connectionFactory != null) { this.connectionFactory.addConnectionListener(this); } } this.initialized = true; }
@Override public void setInterceptors(List<ChannelInterceptor> interceptors) { super.setInterceptors(interceptors); for (ChannelInterceptor interceptor : interceptors) { if (interceptor instanceof ExecutorChannelInterceptor) { this.executorInterceptorsSize++; } } }