有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java严重:无法分派事件:Eventbus com。谷歌。常见的事件巴士。SubscriberExceptionContext

对于EventBus,我将代码合并到我的java Spring应用程序中,并完全控制它,但结果没有改变

当我在spring sts(javaw)中运行EventBus时,除了在带有java-jar项目的服务器中运行之外,没有什么问题。jar会给出同样的严重错误:无法分派事件:错误

下面这句话对我不管用

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无法调用目标事件

包装纸。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;
    }
  }

你看这个方法。调用(目标,新对象[]{event});从方法中抛出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-jar项目运行java时。jar无法发送事件


共 (1) 个答案

  1. # 1 楼答案

    @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());
                }
    
    
    
        }
    

    它总是需要试一试。。谢谢@dwnz的帮助