Spring bean依赖于mule端点

时间:2017-03-14 21:27:13

标签: spring tcp mule lifecycle endpoint

我在Mule中有一个bean需要在关闭时断开TCP端点之前发送消息。 bean实现了Lifecycle并配置了“depends-on = connector”,但似乎“依赖”对关闭顺序没有影响。在我的bean上调用stop方法时,不再连接端点。在端点或连接器上调用之前,有没有办法在我的bean上调用“stop”方法?我正在使用Mule 3.7 CE。

2 个答案:

答案 0 :(得分:0)

您能指定Bean正在实施的生命周期吗?另外,这是一个单例Bean还是原型Bean?

根据文件 - https://docs.mulesoft.com/mule-user-guide/v/3.7/developing-components#component-lifecycle

您可以实现接口org.mule.api.lifecycle.Callable,只需使用onCall方法,即可触发任何操作。

示例:

   public class MyClass implements Callable {

@Override
public Object onCall(MuleEventContext eventContext) throws Exception {
    //DO SOMETHING
    //Call STOP method
    return eventContext.getMessage().getPayload();
}}

答案 1 :(得分:0)

尝试侦听MuleContextNotification并等待CONTEXT_STOPPING操作来触发TCP调用。

见下面的例子:

public class MuleContextListener implements MuleContextNotificationListener<MuleContextNotification>,MuleContextAware {

    private MuleContext context;
    private final static Logger logger = LogManager.getLogger(MuleContextListener.class);

    @Override
    public void onNotification(MuleContextNotification notification) {
        logger.info("NOTIFICATION RECEIVED!!!!!");
        logger.info(MuleContextNotification.getActionName(notification.getAction()));
        if(notification.getAction() == MuleContextNotification.CONTEXT_STOPPING ){
            //replace with your tcp call 
            logger.info(" MY ACTION HAS BEEN TRIGGER");
        }
    }

    @Override
    public void setMuleContext(MuleContext context) {
        this.context = context;
    }
}

为了测试,我有另一个实现生命周期Stoppable的bean ...

public class ClassStopLc implements Stoppable {

    @Override
    public void stop() throws MuleException {
        logger.info(" ClassStopLC Stopping");
    }

如果我查看我的日志:

2017-03-22 16:08:26,827 [[mulecontextaware].mulecontextawareFlow.1.02] INFO  mulecontextaware.MuleContextListener - NOTIFICATION RECEIVED!!!!!
2017-03-22 16:08:26,827 [[mulecontextaware].mulecontextawareFlow.1.02] INFO  mulecontextaware.MuleContextListener - mule context stopping
2017-03-22 16:08:26,827 [[mulecontextaware].mulecontextawareFlow.1.02] INFO  mulecontextaware.MuleContextListener -  MY ACTION HAS BEEN TRIGGER
.
.
.
2017-03-22 16:08:32,077 [[mulecontextaware].mulecontextawareFlow.1.02] INFO  org.mule.util.queue.QueueXaResourceManager - Stopping ResourceManager
2017-03-22 16:08:32,077 [[mulecontextaware].mulecontextawareFlow.1.02] INFO  org.mule.util.queue.QueueXaResourceManager - Stopped ResourceManager
2017-03-22 16:08:32,080 [[mulecontextaware].mulecontextawareFlow.1.02] INFO  mulecontextaware.ClassStopLc -  ClassStopLC Stopping
2017-03-22 16:08:32,082 [[mulecontextaware].mulecontextawareFlow.1.02] INFO  mulecontextaware.MuleContextListener - NOTIFICATION RECEIVED!!!!!
2017-03-22 16:08:32,082 [[mulecontextaware].mulecontextawareFlow.1.02] INFO  mulecontextaware.MuleContextListener - mule context stopped
2017-03-22 16:08:32,083 [[mulecontextaware].mulecontextawareFlow.1.02] INFO  mulecontextaware.MuleContextListener - NOTIFICATION RECEIVED!!!!!
2017-03-22 16:08:32,083 [[mulecontextaware].mulecontextawareFlow.1.02] INFO  mulecontextaware.MuleContextListener - mule context disposing

不确定停止阶段是否允许您毫无问题地拨打TCP电话,您是否可以尝试让我知道?我很好奇:)