use of org.springframework.integration.core.MessageProducer in project spring-integration by spring-projects.
the class ManualFlowTests method testMessageProducerForOutputChannel.
@Test
public void testMessageProducerForOutputChannel() {
class MessageProducingHandler implements MessageHandler, MessageProducer {
private MessageChannel outputChannel;
@Override
public void setOutputChannel(MessageChannel outputChannel) {
this.outputChannel = outputChannel;
}
@Override
public MessageChannel getOutputChannel() {
return this.outputChannel;
}
@Override
public void handleMessage(Message<?> message) throws MessagingException {
this.outputChannel.send(message);
}
}
PollableChannel resultChannel = new QueueChannel();
IntegrationFlowRegistration flowRegistration = this.integrationFlowContext.registration(flow -> flow.handle(new MessageProducingHandler()).channel(resultChannel)).register();
this.integrationFlowContext.messagingTemplateFor(flowRegistration.getId()).send(new GenericMessage<>("test"));
Message<?> receive = resultChannel.receive(1000);
assertNotNull(receive);
assertEquals("test", receive.getPayload());
}
use of org.springframework.integration.core.MessageProducer in project spring-integration by spring-projects.
the class IntegrationFlowDefinition method registerOutputChannelIfCan.
private B registerOutputChannelIfCan(MessageChannel outputChannel) {
if (!(outputChannel instanceof FixedSubscriberChannelPrototype)) {
this.integrationComponents.put(outputChannel, null);
if (this.currentComponent != null) {
String channelName = null;
if (outputChannel instanceof MessageChannelReference) {
channelName = ((MessageChannelReference) outputChannel).getName();
}
Object currentComponent = this.currentComponent;
if (AopUtils.isAopProxy(currentComponent)) {
currentComponent = extractProxyTarget(currentComponent);
}
if (currentComponent instanceof MessageProducer) {
MessageProducer messageProducer = (MessageProducer) currentComponent;
checkReuse(messageProducer);
if (channelName != null) {
if (messageProducer instanceof AbstractMessageProducingHandler) {
((AbstractMessageProducingHandler) messageProducer).setOutputChannelName(channelName);
} else {
throw new BeanCreationException("The 'currentComponent' (" + currentComponent + ") must extend 'AbstractMessageProducingHandler' " + "for message channel resolution by name.\n" + "Your handler should extend 'AbstractMessageProducingHandler', " + "its subclass 'AbstractReplyProducingMessageHandler', or you should " + "reference a 'MessageChannel' bean instead of its name.");
}
} else {
messageProducer.setOutputChannel(outputChannel);
}
} else if (currentComponent instanceof SourcePollingChannelAdapterSpec) {
SourcePollingChannelAdapterFactoryBean pollingChannelAdapterFactoryBean = ((SourcePollingChannelAdapterSpec) currentComponent).get().getT1();
if (channelName != null) {
pollingChannelAdapterFactoryBean.setOutputChannelName(channelName);
} else {
pollingChannelAdapterFactoryBean.setOutputChannel(outputChannel);
}
} else {
throw new BeanCreationException("The 'currentComponent' (" + currentComponent + ") is a one-way 'MessageHandler' and it isn't appropriate to configure 'outputChannel'. " + "This is the end of the integration flow.");
}
this.currentComponent = null;
}
}
return _this();
}
use of org.springframework.integration.core.MessageProducer in project spring-integration by spring-projects.
the class MockIntegrationContext method substituteMessageHandlerFor.
public void substituteMessageHandlerFor(String consumerEndpointId, MessageHandler mockMessageHandler, boolean autoStartup) {
Object endpoint = this.beanFactory.getBean(consumerEndpointId, IntegrationConsumer.class);
if (autoStartup && endpoint instanceof Lifecycle) {
((Lifecycle) endpoint).stop();
}
DirectFieldAccessor directFieldAccessor = new DirectFieldAccessor(endpoint);
Object targetMessageHandler = directFieldAccessor.getPropertyValue("handler");
this.beans.put(consumerEndpointId, targetMessageHandler);
if (mockMessageHandler instanceof MessageProducer) {
if (targetMessageHandler instanceof MessageProducer) {
MessageChannel outputChannel = ((MessageProducer) targetMessageHandler).getOutputChannel();
((MessageProducer) mockMessageHandler).setOutputChannel(outputChannel);
} else {
if (mockMessageHandler instanceof MockMessageHandler) {
if (TestUtils.getPropertyValue(mockMessageHandler, "hasReplies", Boolean.class)) {
throw new IllegalStateException("The [" + mockMessageHandler + "] " + "with replies can't replace simple MessageHandler [" + targetMessageHandler + "]");
}
} else {
throw new IllegalStateException("The MessageProducer handler [" + mockMessageHandler + "] " + "can't replace simple MessageHandler [" + targetMessageHandler + "]");
}
}
}
directFieldAccessor.setPropertyValue("handler", mockMessageHandler);
if (autoStartup && endpoint instanceof Lifecycle) {
((Lifecycle) endpoint).start();
}
}
use of org.springframework.integration.core.MessageProducer in project spring-integration by spring-projects.
the class IntegrationMBeanExporter method afterSingletonsInstantiated.
@Override
public void afterSingletonsInstantiated() {
Map<String, MessageHandlerMetrics> messageHandlers = this.applicationContext.getBeansOfType(MessageHandlerMetrics.class);
for (Entry<String, MessageHandlerMetrics> entry : messageHandlers.entrySet()) {
String beanName = entry.getKey();
MessageHandlerMetrics bean = entry.getValue();
if (this.handlerInAnonymousWrapper(bean) != null) {
if (logger.isDebugEnabled()) {
logger.debug("Skipping " + beanName + " because it wraps another handler");
}
continue;
}
// If the handler is proxied, we have to extract the target to expose as an MBean.
// The MetadataMBeanInfoAssembler does not support JDK dynamic proxies.
MessageHandlerMetrics monitor = (MessageHandlerMetrics) extractTarget(bean);
this.handlers.add(monitor);
}
Map<String, MessageSourceMetrics> messageSources = this.applicationContext.getBeansOfType(MessageSourceMetrics.class);
for (Entry<String, MessageSourceMetrics> entry : messageSources.entrySet()) {
// If the source is proxied, we have to extract the target to expose as an MBean.
// The MetadataMBeanInfoAssembler does not support JDK dynamic proxies.
MessageSourceMetrics monitor = (MessageSourceMetrics) extractTarget(entry.getValue());
this.sources.add(monitor);
}
Map<String, MessageChannelMetrics> messageChannels = this.applicationContext.getBeansOfType(MessageChannelMetrics.class);
for (Entry<String, MessageChannelMetrics> entry : messageChannels.entrySet()) {
// If the channel is proxied, we have to extract the target to expose as an MBean.
// The MetadataMBeanInfoAssembler does not support JDK dynamic proxies.
MessageChannelMetrics monitor = (MessageChannelMetrics) extractTarget(entry.getValue());
this.channels.add(monitor);
}
Map<String, MessageProducer> messageProducers = this.applicationContext.getBeansOfType(MessageProducer.class);
for (Entry<String, MessageProducer> entry : messageProducers.entrySet()) {
MessageProducer messageProducer = entry.getValue();
if (messageProducer instanceof Lifecycle) {
Lifecycle target = (Lifecycle) extractTarget(messageProducer);
if (!(target instanceof AbstractMessageProducingHandler)) {
this.inboundLifecycleMessageProducers.add(target);
}
}
}
super.afterSingletonsInstantiated();
try {
registerChannels();
registerHandlers();
registerSources();
registerEndpoints();
if (this.applicationContext.containsBean(IntegrationContextUtils.INTEGRATION_MESSAGE_HISTORY_CONFIGURER_BEAN_NAME)) {
Object messageHistoryConfigurer = this.applicationContext.getBean(IntegrationContextUtils.INTEGRATION_MESSAGE_HISTORY_CONFIGURER_BEAN_NAME);
if (messageHistoryConfigurer instanceof MessageHistoryConfigurer) {
registerBeanInstance(messageHistoryConfigurer, IntegrationContextUtils.INTEGRATION_MESSAGE_HISTORY_CONFIGURER_BEAN_NAME);
}
}
if (!this.applicationContext.containsBean(IntegrationManagementConfigurer.MANAGEMENT_CONFIGURER_NAME)) {
this.managementConfigurer = new IntegrationManagementConfigurer();
this.managementConfigurer.setDefaultCountsEnabled(true);
this.managementConfigurer.setDefaultStatsEnabled(true);
this.managementConfigurer.setApplicationContext(this.applicationContext);
this.managementConfigurer.setBeanName(IntegrationManagementConfigurer.MANAGEMENT_CONFIGURER_NAME);
this.managementConfigurer.afterSingletonsInstantiated();
} else {
this.managementConfigurer = this.applicationContext.getBean(IntegrationManagementConfigurer.MANAGEMENT_CONFIGURER_NAME, IntegrationManagementConfigurer.class);
}
} catch (RuntimeException e) {
unregisterBeans();
throw e;
}
}
Aggregations