这是我第一次尝试使用 ControlBus 打开/关闭入站 channel 适配器。
为简单起见,我通过将一个空文件写入一个由入站文件适配器监视的目录(这个永远打开)来发送我的控制消息,然后我将其路由到一个将打开的服务激活器/关闭我的其他入站适配器。
<int:channel id="controlBusChannel" />
<int:control-bus input-channel="controlBusChannel" auto-startup="true"/>
<file:inbound-channel-adapter id="controlFilesIn" directory="file:/tmp/control/input" prevent-duplicates="true" auto-startup="true">
<int:poller id="poller" fixed-delay="500"/>
</file:inbound-channel-adapter>
<int:service-activator input-channel="controlFilesIn" output-channel="controlFilesOut" ref="controlFileHandler" method="handleUpdate"/>
<bean id="controlFileHandler" class="com.myproj.integration.ControlBusController"/>
<file:outbound-channel-adapter id="controlFilesOut" directory="file:/tmp/control/output" delete-source-files="true" />
<file:inbound-channel-adapter id="filesIn" directory="file:/tmp/filesIn/input" prevent-duplicates="true" filter="FileFilterOnLastModifiedTime" auto-startup="false">
<int:poller id="poller" fixed-delay="500"/>
</file:inbound-channel-adapter>
在我的 ControlBusController bean 中:
@Component
public class ControlBusController implements ApplicationContextAware {
final static Logger logger = LoggerFactory.getLogger(ControlBusController.class);
private ApplicationContext ctx;
public File handleUpdate(File input) throws ParseException, IOException, FileNotFoundException {
String fileName = input.getName();
logger.info("===================================");
logger.info("Triggering control bus update by " + fileName);
String[] fnArray = fileName.split("_");
String inputChannel = fnArray[1];
String inputCommand = fnArray[2];
if ("FILESIN".equals(inputChannel) && "START".equals(inputCommand)) {
MessageChannel channel = ctx.getBean("controlBusChannel", MessageChannel.class);
if (channel != null) {
String controlMessage = "@filesIn.start()";
logger.info("Sending control message: " + controlMessage);
channel.send(new GenericMessage<>(controlMessage));
} else logger.error("Could not get Message Channel from context or context was null");
}
return input;
}
@Override
public void setApplicationContext(ApplicationContext ac) throws BeansException {
this.ctx = ac;
}
}
消息被发送到 controlBusChannel,但我收到一条错误消息:
Caused by: org.springframework.expression.EvaluationException: The method 'start' is not supported by this command processor. If using the Control Bus, consider adding @ManagedOperation or @ManagedAttribute.
at org.springframework.integration.handler.ExpressionCommandMessageProcessor$ExpressionCommandMethodResolver.validateMethod(ExpressionCommandMessageProcessor.java:111)
filesIn 已声明为我的适配器的 ID,如上面的 XML 片段所示。
有什么想法吗?谢谢!
附言我试过只放入 @ManagedOperation/@ManagedAttribute 注释,但它们似乎没有任何积极效果。
最佳答案
因为你没有 channel
<int-file:inbound-channel-adapter>
上的属性, 但只是一个 id
, 所以 channel 是自动创建的,它与 id
完全一致.适配器本身有一个像 filesIn.adapter
这样的 bean 名称。 .
因此,您必须将 ControlBus 命令表达式更改为:
String controlMessage = "@'filesIn.adapter'.start()";
关于java - 使用 ControlBus 发送消息以打开 Spring Integration 入站适配器时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30374794/