Mule 动态添加Endpoint

Muel 可以通过MuleContext来动态的添加serviceendpoint等对象,主要原理是:拿到一个已经配好的现成的endpoint,然后对其克隆,做出自己需要的endpoint,然后在添加到需要动态添加endpointservice中。

方法:

首先配置一个Globalendpoint,配置Global原因是需要通过MuleRegistry

lookupEndpointBuilder方法获得endpoint,而该方法的说明如下:

Looks-up endpoint builders which can be used to repeatably create endpoints with the same configuration. These endpoint builder are either global endpoints or they are builders used to create named endpoints configured on routers and exception strategies.

然后,代码部分,这里以Filetransport做例子,其他原理一样:

1.   //获得需要的service

2.   Service service = muleContext.getRegistry().lookupService("GetFile");

3.   //获得定义的Global endpoint作为模板

4.   EndpointBuilder endpointBuilder =
    muleContext.getRegistry().lookupEndpointBuilder("file1");

5.   //克隆一份

6.   EndpointBuilder endpointBuilder2 = (EndpointBuilder) endpointBuilder.clone();

7.   //放入自己需要的新的信息

8.   endpointBuilder2.setURIBuilder(new URIBuilder("file:///D:/temp/temp2/"));

9.   //注册新的endpoint

10. muleContext.getRegistry().registerEndpointBuilder("file2", endpointBuilder2);

11. //添加到inbound中,具体按需求

12. service.getInboundRouter().addEndpoint(
    endpointBuilder2.buildInboundEndpoint());

13. //获得FileConnector

14. FileConnector fileConnector =
    (FileConnector)muleContext.getRegistry().lookupConnector("fileConnector");

15. //为新建的endpoint创建receiver

16. MessageReceiver receiver = fileConnector.registerListener(service,
        endpointBuilder2.buildInboundEndpoint());

17. //启动新的receiver

18. receiver.initialise();

19. receiver.connect();

20. receiver.start();

在添加完后,对比现有的endpoint和动态添加的endpoint,会发现,两个endpointname是完全一样的,但在目前为止,并没有发现有什么影响;如果设置不同的name,会有不同的效果。因此大胆猜测为:name的作用为:

1. 与对应的endpoint成键值对匹配,通过name可以找到对应的endpoint。此结论由

muleContext.getRegistry().lookupEndpointBuilder("file1");

证实。

 

2. Mulename对应为一个endpointconfig,当你新建一个endpoint后,通过name的相同,可以将才endpoint配置为同一个config

原文地址:https://www.cnblogs.com/zhuyoufeng/p/2050266.html