Mule 动态添加Endpoint(二)
这次是在Outbound中动态修改endpoint。以文件的移动为例。上代码:
file-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesource.org/schema/mule/core/2.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:file="http://www.mulesource.org/schema/mule/file/2.2"
xsi:schemaLocation="
http://www.mulesource.org/schema/mule/core/2.2 http://www.mulesource.org/schema/mule/core/2.2/mule.xsd
http://www.mulesource.org/schema/mule/file/2.2 http://www.mulesource.org/schema/mule/file/2.2/mule-file.xsd
">
<file:connector name="fileConnector" streaming="false" pollingFrequency="1000" autoDelete="true"
outputPattern="#[ORIGINALNAME]"/>
<endpoint name="origin" address="file:///D:/testDir/1" connector-ref="fileConnector"/>
<endpoint name="target" address="file:///D:/testDir/3" connector-ref="fileConnector"/>
<model name="testModel">
<service name="testService">
<inbound>
<file:inbound-endpoint ref="origin"/>
</inbound>
<outbound>
<pass-through-router>
<file:outbound-endpoint ref="target"/>
</pass-through-router>
</outbound>
</service>
</model>
</mule>
<mule xmlns="http://www.mulesource.org/schema/mule/core/2.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:file="http://www.mulesource.org/schema/mule/file/2.2"
xsi:schemaLocation="
http://www.mulesource.org/schema/mule/core/2.2 http://www.mulesource.org/schema/mule/core/2.2/mule.xsd
http://www.mulesource.org/schema/mule/file/2.2 http://www.mulesource.org/schema/mule/file/2.2/mule-file.xsd
">
<file:connector name="fileConnector" streaming="false" pollingFrequency="1000" autoDelete="true"
outputPattern="#[ORIGINALNAME]"/>
<endpoint name="origin" address="file:///D:/testDir/1" connector-ref="fileConnector"/>
<endpoint name="target" address="file:///D:/testDir/3" connector-ref="fileConnector"/>
<model name="testModel">
<service name="testService">
<inbound>
<file:inbound-endpoint ref="origin"/>
</inbound>
<outbound>
<pass-through-router>
<file:outbound-endpoint ref="target"/>
</pass-through-router>
</outbound>
</service>
</model>
</mule>
FileClient.java
1 import org.mule.api.MuleContext;
2 import org.mule.api.endpoint.EndpointBuilder;
3 import org.mule.api.routing.OutboundRouter;
4 import org.mule.api.service.Service;
5 import org.mule.context.DefaultMuleContextFactory;
6 import org.mule.endpoint.URIBuilder;
7
8 /**
9 * @Class name: FileClient
10 *
11 * Dynamic add endpoint for OutboundRouter.
12 * @author: youfeng
13 * @modified: 8/5/11
14 */
15 public class FileClient {
16
17 public static void main(String[] args) throws Throwable {
18 try {
19 //start the mule server
20 MuleContext muleContext;
21 muleContext = new DefaultMuleContextFactory().createMuleContext("file-config.xml");
22 muleContext.start();
23
24 //Get the target Service
25 Service service = muleContext.getRegistry().lookupService("testService");
26
27 //Clone a Global service
28 EndpointBuilder endpointBuilder = muleContext.getRegistry().lookupEndpointBuilder("origin");
29 EndpointBuilder cloneEndpoint = (EndpointBuilder) endpointBuilder.clone();
30
31 //Rewrite the info for clone endpoint
32 cloneEndpoint.setURIBuilder(new URIBuilder("file:///D:/testDir/4"));
33 muleContext.getRegistry().registerEndpointBuilder("targetDir2", cloneEndpoint);
34
35 //Get the OutboundRouter, clear the message and add the new endpoint
36 OutboundRouter outboundRouter = ((OutboundRouter) service.getOutboundRouter().getRouters().get(0));
37 outboundRouter.getEndpoints().clear();
38 outboundRouter.addEndpoint(cloneEndpoint.buildOutboundEndpoint());
39
40 //restart the service
41 service.stop();
42 service.start();
43
44
45 } catch (Exception e) {
46 e.printStackTrace();
47 }
48 }
49 }
2 import org.mule.api.endpoint.EndpointBuilder;
3 import org.mule.api.routing.OutboundRouter;
4 import org.mule.api.service.Service;
5 import org.mule.context.DefaultMuleContextFactory;
6 import org.mule.endpoint.URIBuilder;
7
8 /**
9 * @Class name: FileClient
10 *
11 * Dynamic add endpoint for OutboundRouter.
12 * @author: youfeng
13 * @modified: 8/5/11
14 */
15 public class FileClient {
16
17 public static void main(String[] args) throws Throwable {
18 try {
19 //start the mule server
20 MuleContext muleContext;
21 muleContext = new DefaultMuleContextFactory().createMuleContext("file-config.xml");
22 muleContext.start();
23
24 //Get the target Service
25 Service service = muleContext.getRegistry().lookupService("testService");
26
27 //Clone a Global service
28 EndpointBuilder endpointBuilder = muleContext.getRegistry().lookupEndpointBuilder("origin");
29 EndpointBuilder cloneEndpoint = (EndpointBuilder) endpointBuilder.clone();
30
31 //Rewrite the info for clone endpoint
32 cloneEndpoint.setURIBuilder(new URIBuilder("file:///D:/testDir/4"));
33 muleContext.getRegistry().registerEndpointBuilder("targetDir2", cloneEndpoint);
34
35 //Get the OutboundRouter, clear the message and add the new endpoint
36 OutboundRouter outboundRouter = ((OutboundRouter) service.getOutboundRouter().getRouters().get(0));
37 outboundRouter.getEndpoints().clear();
38 outboundRouter.addEndpoint(cloneEndpoint.buildOutboundEndpoint());
39
40 //restart the service
41 service.stop();
42 service.start();
43
44
45 } catch (Exception e) {
46 e.printStackTrace();
47 }
48 }
49 }
示例代码下载: