Mule 控制Service的启/止
使用mule-module-management控制
操作如下:
首先:加入mule-module-management的依赖
1. <dependency>
2. <groupId>org.mule.modules</groupId>
3. <artifactId>mule-module-management</artifactId>
4. <version>${mule.version}</version>
5. </dependency>
然后,配置mule 配置文件:
1. <management:jmx-default-config port="8089">
2. <management:credentials>
3. <spring:entry key="test" value="test"/>
4. </management:credentials>
5. </management:jmx-default-config>
6. <management:jmx-mx4j-adaptor jmxAdaptorUrl="http://127.0.0.1:8089"
7. login="test" password="test"/>
在浏览器中输入http://127.0.0.1:8089 即可
使用MuleContextAware控制muleContext
自定义一个service,用于启/止service,当需要的时候,向该service发送一个指令,即可实现。配置如下:
1. <service name="invokes">
2. <inbound>
3. <vm:inbound-endpoint path="invoke"/>
4. </inbound>
5. <component class="manager.MuleContextImpl">
6. <method-entry-point-resolver>
7. <include-entry-point method="stop"/>
8. </method-entry-point-resolver>
9. </component>
10. </service>
实现类:
1. public class MuleContextImpl implements MuleContextAware {
2. private org.mule.api.MuleContext muleContext;
3.
4. public void setMuleContext(org.mule.api.MuleContext muleContext) {
5. this.muleContext = muleContext;
6. }
7.
8. public Object stop(Map map) {
9. String operate = (String) map.get("operate");
10. String serviceName = (String) map.get("service");
11. try {
12. Service service =
muleContext.getRegistry().lookupService(serviceName);
13. if ("start".equals(operate)) {
14. service.start();
15. } else if ("stop".equals(operate)) {
16. service.stop();
17. }
18. } catch (MuleException e) {
19. e.printStackTrace();
20. }
21. return "abc";
22. }
23. }
定义一个公共的类
定义一个公共的类,存放MuleContext,在需要使用时,调用MuleContext。