使用配置了属性的多个应用程序上下文。
这个例子是一个例子。它使用XML进行配置,但是相同的技术也适用于Java配置。
如果您需要他们共同养活
emailReceiverService; 使各个适配器上下文成为子上下文;请参阅样本自述文件以获取有关如何进行操作的指针。
编辑:
这是一个示例,其中服务(和渠道)在共享的父上下文中…
@Configuration@EnableIntegrationpublic class MultiImapAdapter { public static void main(String[] args) throws Exception { AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext(MultiImapAdapter.class); parent.setId("parent"); String[] urls = { "imap://foo", "imap://bar" }; List<ConfigurableApplicationContext> children = new ArrayList<ConfigurableApplicationContext>(); int n = 0; for (String url : urls) { AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext(); child.setId("child" + ++n); children.add(child); child.setParent(parent); child.register(GeneralImapAdapter.class); StandardEnvironment env = new StandardEnvironment(); Properties props = new Properties(); // populate properties for this adapter props.setProperty("imap.url", url); PropertiesPropertySource pps = new PropertiesPropertySource("imapprops", props); env.getPropertySources().addLast(pps); child.setEnvironment(env); child.refresh(); } System.out.println("Hit enter to terminate"); System.in.read(); for (ConfigurableApplicationContext child : children) { child.close(); } parent.close(); } @Bean public MessageChannel emailChannel() { return new DirectChannel(); } @Bean public EmailReceiverService emailReceiverService() { return new EmailReceiverService(); }}和
@Configuration@EnableIntegrationpublic class GeneralImapAdapter { @Bean public static PropertySourcesPlaceholderConfigurer pspc() { return new PropertySourcesPlaceholderConfigurer(); } @Bean @InboundChannelAdapter(value = "emailChannel", poller = @Poller(fixedDelay = "10000") ) public MessageSource<javax.mail.Message> mailMessageSource(MailReceiver imapMailReceiver) { return new MailReceivingMessageSource(imapMailReceiver); } @Bean @Value("${imap.url}") public MailReceiver imapMailReceiver(String imapUrl) {// ImapMailReceiver imapMailReceiver = new ImapMailReceiver(imapUrl);// imapMailReceiver.setShouldMarkMessagesAsRead(true);// imapMailReceiver.setShouldDeleteMessages(false);// // other setters here// return imapMailReceiver; MailReceiver receiver = mock(MailReceiver.class); Message message = mock(Message.class); when(message.toString()).thenReturn("Message from " + imapUrl); Message[] messages = new Message[] {message}; try { when(receiver.receive()).thenReturn(messages); } catch (MessagingException e) { e.printStackTrace(); } return receiver; }}和
@MessageEndpointpublic class EmailReceiverService { @ServiceActivator(inputChannel="emailChannel") public void handleMessage(javax.mail.Message message) { System.out.println(message); }}希望能有所帮助。
请注意,您不需要服务激活器上的轮询器-使用a
DirectChannel,该服务将在轮询器执行器线程上被调用-无需其他异步切换。



