我需要能够将转换器添加到由MVC应用程序管理的正在运行的Spring集成应用程序上下文中,为了实现这一点,我从一个简单的POC开始,我在运行时加载一个新的转换器(这个转换器不是在启动时扫描),这个POC作为一个独立的Java应用程序运行,只包含spring集成库,这里是主类。
@Component
public class DynamicApplication {
@Autowired
private ConfigurableApplicationContext appContext;
@Autowired
private BeanFactory beanFactory;
private Map<String, String> channelsMap = new HashMap<String, String>();
private Map<String, String> inboundGWMap = new HashMap<String, String>();
public static void main(String a[]) throws Exception{
DynamicApplication dynamicApplication = new DynamicApplication();
GenericXmlApplicationContext context = dynamicApplication.setupContext();
DynamicApplication localDynamicApplication = context.getBean(DynamicApplication.class);
//then we wait for input to add new classes
final Scanner scanner = new Scanner(System.in);
while (true) {
final String input = scanner.nextLine();
if("q".equals(input.trim())) {
break;
}
else{
//in the case we got a package definition to be scanned
localDynamicApplication.addClassesFromAnnotatedPackage(input);
}
}
}
public GenericXmlApplicationContext setupContext() {
final GenericXmlApplicationContext context = new GenericXmlApplicationContext();
context.load("configuration/inbound-grand-central-configuration.xml");
context.registerShutdownHook();
context.refresh();
DynamicApplication dynamicApplication = context.getBean(DynamicApplication.class);
for(int i = 0 ; i < 1 ; i++){
TcpInboundGateway listener = dynamicApplication.createTcpInboundGateway("server" + i, 9877 + i);
listener.start();
}
return context;
}
}
一个非常简单的服务激活器
package com.client.connector.inbound.config;
@MessageEndpoint
public class BussinesService {
@ServiceActivator(inputChannel="toBSChannel")
public String processIncomingMessage(String message) {
System.out.println("Bussines logic: " + message);
return "respondemos:" + message;
}
}
还有一个初始转换器 包com.client.connector.inbound.config;
@MessageEndpoint
public class ISOConverter {
@Transformer(inputChannel="iSOConverterChannel", outputChannel="toBSChannel")
public String iSOConverter(Object payload) throws Exception {
System.out.println("ISO Conversion place");
if (payload instanceof byte[]) {
return new String((byte[]) payload);
}
else if (payload instanceof char[]) {
return new String((char[]) payload);
}
else {
return payload.toString();
}
}
@Transformer(inputChannel="iSOConverterChannel", outputChannel="toBSChannel")
public String iSOConverter(byte[] payload) throws Exception {
System.out.println("ISO Conversion place");
if (payload instanceof byte[]) {
return new String((byte[]) payload);
}
else {
return payload.toString();
}
}
}
和xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
..............................
............................">
<description>Inbound Connectors common configuration file.
</description>
<int:annotation-config />
<context:component-scan base-package="com.client.connector.inbound" />
</beans>
然后我有另一个在启动时未扫描的转换器。
package com.client.inboundtest.newclass;
@MessageEndpoint
public class DinamicAddedConverter {
@Transformer(inputChannel="iSOConverterChannel", outputChannel="toBSChannel")
public String iSOConverter2(Object payload) throws Exception {
System.out.println("ISO conversion from new class");
if (payload instanceof byte[]) {
return new String((byte[]) payload);
}
else if (payload instanceof char[]) {
return new String((char[]) payload);
}
else {
return payload.toString();
}
}
@Transformer(inputChannel="iSOConverterChannel", outputChannel="toBSChannel")
public String iSOConverter2(byte[] payload) throws Exception {
System.out.println("ISO conversion from new class");
if (payload instanceof byte[]) {
return new String((byte[]) payload);
}
else {
return payload.toString();
}
}
}
当我启动程序,并使用telnet连接到端口9877,并设置单词test时,我在java app的输出中得到了这个
ISO Conversion place
Bussines logic: test
ISO Conversion place
Bussines logic: test
如果我在java控制台中编写com.client.inboundtest.newclass包,它会加载新的转换器,并显示日志
com.client.inboundtest.newclass
Added class com.client.inboundtest.newclass.DinamicAddedConverter
Nov 11, 2015 7:41:51 PM org.springframework.integration.endpoint.EventDrivenConsumer logComponentSubscriptionEvent
INFO: Adding {transformer:com.client.inboundtest.newclass.DinamicAddedConverter.iSOConverter2.transformer} as a subscriber to the 'iSOConverterChannel' channel
Nov 11, 2015 7:41:51 PM org.springframework.integration.channel.AbstractSubscribableChannel adjustCounterIfNecessary
INFO: Channel 'org.springframework.context.support.GenericXmlApplicationContext@4d9cad9d.iSOConverterChannel' has 3 subscriber(s).
Nov 11, 2015 7:41:51 PM org.springframework.integration.endpoint.AbstractEndpoint start
INFO: started com.client.inboundtest.newclass.DinamicAddedConverter.iSOConverter2.transformer
Nov 11, 2015 7:41:51 PM org.springframework.integration.endpoint.EventDrivenConsumer logComponentSubscriptionEvent
INFO: Adding {transformer} as a subscriber to the 'iSOConverterChannel' channel
Nov 11, 2015 7:41:51 PM org.springframework.integration.channel.AbstractSubscribableChannel adjustCounterIfNecessary
INFO: Channel 'org.springframework.context.support.GenericXmlApplicationContext@4d9cad9d.iSOConverterChannel' has 4 subscriber(s).
Nov 11, 2015 7:41:51 PM org.springframework.integration.endpoint.AbstractEndpoint start
INFO: started com.client.inboundtest.newclass.DinamicAddedConverter.iSOConverter2.transformer#2
并向同一个打开的telnet连接发送新消息,在java app console
上提供此输出ISO Conversion place
Bussines logic: test2
ISO Conversion place
Bussines logic: test2
ISO conversion from new class
Bussines logic: test2
ISO conversion from new class
Bussines logic: test2
这是我的期望。
然后我尝试将我的POC移动到Spring MVC上下文中我开始看到意外的行为,第一个非预期的事情是通道不是动态创建的,因为它发生在第一个POC中,所以我需要定义它们在像
这样的上下文文件中是显式的<beans ........">
<context:component-scan base-package="main.package" />
<int:annotation-config />
<int:channel id="toBSChannel" />
<int:channel id="iSOConverterChannel" />
<int:channel id="restRequestChannel" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
我所做的是创建一个连接到我的原始DynamicApplication的新控制器,并在启动监听器的bean子上通过@PostConstruct将静态调用更改为setup。
当应用程序启动时,它会在端口9877上显示我的serlvlets和我的监听器,当我调用刷新servlet时,它显示的是我从java应用程序获得的完全相同的输出:
Added class com.client.inboundtest.newclass.ConverterA
Nov 11, 2015 7:55:59 PM org.springframework.integration.endpoint.EventDrivenConsumer logComponentSubscriptionEvent
INFO: Adding {transformer:com.client.inboundtest.newclass.ConverterA.iSOConverter.transformer} as a subscriber to the 'iSOConverterChannel' channel
Nov 11, 2015 7:55:59 PM org.springframework.integration.channel.AbstractSubscribableChannel adjustCounterIfNecessary
INFO: Channel 'org.springframework.web.context.WebApplicationContext:/InboundDinamicGC/dispatcher.iSOConverterChannel' has 7 subscriber(s).
Nov 11, 2015 7:55:59 PM org.springframework.integration.endpoint.AbstractEndpoint start
INFO: started com.client.inboundtest.newclass.ConverterA.iSOConverter.transformer
Nov 11, 2015 7:55:59 PM org.springframework.integration.endpoint.EventDrivenConsumer logComponentSubscriptionEvent
INFO: Adding {transformer} as a subscriber to the 'iSOConverterChannel' channel
Nov 11, 2015 7:55:59 PM org.springframework.integration.channel.AbstractSubscribableChannel adjustCounterIfNecessary
INFO: Channel 'org.springframework.web.context.WebApplicationContext:/InboundDinamicGC/dispatcher.iSOConverterChannel' has 8 subscriber(s).
Nov 11, 2015 7:55:59 PM org.springframework.integration.endpoint.AbstractEndpoint start
INFO: started com.client.inboundtest.newclass.ConverterA.iSOConverter.transformer#2
问题是消息永远不会到达新的转换器,因为它发生在普通的Java POC上,任何想法?
答案 0 :(得分:1)
请查看类似问题How to hook up a list of message driven adapters without actually writing each one out?及其讨论,以及其中的链接。
您应该做的只是创建一个child
应用程序上下文。就像你在setupContext()
中所做的一样,但当然要推动那个MVC parent
和refresh()
。