栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Spring多个imapAdapter

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring多个imapAdapter

使用配置了属性的多个应用程序上下文。

这个例子是一个例子。它使用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
,该服务将在轮询器执行器线程上被调用-无需其他异步切换。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/378791.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号