Spring bean 依赖于 mule 端点
Spring bean depends on mule endpoint
我在 Mule 中有一个 bean,它需要在 TCP 端点在关闭时断开连接之前发送消息。该 bean 实现了 Lifecycle 并配置了 "depends-on=connector," 但似乎 "depends-on" 对关闭顺序没有影响。在我的 bean 上调用 stop 方法时,端点不再连接。在端点或连接器上调用 "stop" 方法之前,是否可以在我的 bean 上调用它?我正在使用 Mule 3.7 CE。
您能否指定您的 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();
}}
尝试监听 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;
}
}
为了测试,我有另一个实现生命周期可停止的 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 调用,您可以试试让我知道吗?我很好奇:)
我在 Mule 中有一个 bean,它需要在 TCP 端点在关闭时断开连接之前发送消息。该 bean 实现了 Lifecycle 并配置了 "depends-on=connector," 但似乎 "depends-on" 对关闭顺序没有影响。在我的 bean 上调用 stop 方法时,端点不再连接。在端点或连接器上调用 "stop" 方法之前,是否可以在我的 bean 上调用它?我正在使用 Mule 3.7 CE。
您能否指定您的 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();
}}
尝试监听 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;
}
}
为了测试,我有另一个实现生命周期可停止的 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 调用,您可以试试让我知道吗?我很好奇:)