对于EventBus,我将我的
java
Spring应用程序中的代码合并,并完全控制它,但结果没有改变.
当我在Spring sts(javaw)中运行EventBus时,没有问题,但是当我使用java -jar project.jar运行在服务器中时,它给出相同的SEVERE:无法分派事件:错误
下面没有为我工作
package edu.uams.event; import java.awt.EventQueue; import java.lang.reflect.InvocationTargetException; import java.util.concurrent.Executor; import org.apache.log4j.Logger; import com.google.common.eventbus.AsyncEventBus; import com.google.common.eventbus.EventHandler; import com.google.common.eventbus.SubscriberExceptionHandler; import edu.uams.domain.TirEvent; import edu.uams.pacs.IncomingFileMonitor; public class AysncTraumaEventBus extends AsyncEventBus { private final static Logger logger = Logger.getLogger(AysncTraumaEventBus.class); private String name = null; public AysncTraumaEventBus(Executor executor,SubscriberExceptionHandler subscriberExceptionHandler) { super(executor,subscriberExceptionHandler); logger.info("AysncTraumaEventBus created."); } public AysncTraumaEventBus(String name,Executor executor) { super(name,executor); this.name=name; logger.info("AysncTraumaEventBus created. Name:"+this.name); } @Override public void register(Object object) { super.register(object); } @Override public void unregister(Object object) { super.unregister(object); } @Override public void dispatch(Object event,EventHandler wrapper) { try { logger.info("Let's dispatch Aysnchroneous Trauma Event:"+ ((TirEvent) event).getResultMessage()); wrapper.handleEvent(event); } catch (InvocationTargetException e) { // My logger logger.error("Could not dispatch event: " + event + " to handler " + wrapper+" e:"+e.getMessage()); logger.info("Lets try to disptach again!"); super.post(new ExceptionEvent(event,e)); } } public static final class ExceptionEvent { public final Object event; public final InvocationTargetException exception; public ExceptionEvent(final Object event,final InvocationTargetException exception) { this.event = event; this.exception = exception; } }
}
不知何故,EventHandler无法调用目标事件..
wrapper.handleEvent(事件);
当你看包装(EventHandler):
public void handleEvent(Object event) throws InvocationTargetException { checkNotNull(event); try { method.invoke(target,new Object[] { event }); } catch (IllegalArgumentException e) { throw new Error("Method rejected target/argument: " + event,e); } catch (IllegalAccessException e) { throw new Error("Method became inaccessible: " + event,e); } catch (InvocationTargetException e) { if (e.getCause() instanceof Error) { throw (Error) e.getCause(); } throw e; } }
你看到那个method.invoke(target,new Object [] {event});从Method.class中抛出InvocationTargetException
public Object invoke(Object obj,Object... args) throws IllegalAccessException,IllegalArgumentException,InvocationTargetException { if (!override) { if (!Reflection.quickCheckMemberAccess(clazz,modifiers)) { Class<?> caller = Reflection.getCallerClass(1); checkAccess(caller,clazz,obj,modifiers); } } MethodAccessor ma = methodAccessor; // read volatile if (ma == null) { ma = acquireMethodAccessor(); } return ma.invoke(obj,args); }
不知怎的,它不能调用..但最有趣的部分是同一个jar文件与EventBus可以在STS Run(javaw)中运行良好,但是当我从命令行运行java作为java -jar project.jar时,它不能调度事件..
解决方法
@Subscribe @AllowConcurrentEvents public void receivedDicomFile(TirEvent event){ try { } catch (ClassNotFoundException e) { logger.error(e.getMessage()); } catch (sqlException e) { logger.error(e.getMessage()); } catch(Exception e){ logger.error(e.getMessage()); } }
它总是需要一个try catch ..谢谢@dwnz的帮助