【问题标题】:@MessageDriven bean not receiving messages@MessageDriven bean 没有收到消息
【发布时间】:2013-09-23 20:23:35
【问题描述】:

我根据另一个问题的建议进行了一些更改,将我的班级变成@Singleton 而不是@Stateless,但是我不再像以前那样在@MessageDriven Bean 中接收消息。建议?注意主题在jbossmq-destinations-service.xml中定义正确

@javax.ejb.Singleton(mappedName="MySingletonClass")
public class MySingletonClass implements SomeInterface
{
   private Timer timer = null;

   @javax.annotation.Resource
   TimerService timerService;


   /**
    * Creates the timer.
    */
   @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
   public void resetTimer()
   {
      if (timer != null)
      {
         timer.cancel();
      }
      timer = timerService.createTimer(30000, "Note");
   }

   @Override
   public void readResponseMsg(Document responseXml)
   {
      resetTimer();
      //Do stuff
   }

   @Override
   @Timeout
   public void timeout() throws RemoteException
   {
       //do stuff
   }
}

和 MDB:

@javax.ejb.MessageDriven(mappedName = "jms/MyTopic", activationConfig = {
      @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"),
      @ActivationConfigProperty(propertyName = "destination", propertyValue = "MyTopic"),
      @ActivationConfigProperty(propertyName = "messagingType", propertyValue = "javax.jms.MessageListener"),
      @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge") })
@ResourceAdapter("activemq.rar")
public class MyMDB implements MessageListener
{
   private static final Logger ourLogger = Logger.getLogger(MyMDB.class);

   @javax.ejb.EJB(mappedName="MySingletonClass") private MySingletonClassInterface reader;

   /**
    * @param message
    */
   public void onMessage(Message message)
   {
      TextMessage textMessage = (TextMessage) message;
      try
      {
         System.out.println("Received Message Text: " + textMessage.getText());
         reader.readResponseMsg(loadXMLFromString(textMessage.getText()));
      }
      catch (JMSException | SAXException | ParserConfigurationException | IOException e)
      {
         ourLogger.error("Exception handling Schedule Results Message", e);
      }
   }
}

这是界面:

@javax.ejb.Local
public interface SomeInterface
{
   public void readResponseMsg(Document responseXml);


   public void timeout() throws RemoteException;
}

更新我一直在摆弄一些事情,现在看到一些异常,至少表明正在发生一些事情。我已经用我的更改更新了上面的代码。这是我的例外:

Caused by: javax.naming.NameNotFoundException: MySingletonClass not bound
    at org.jnp.server.NamingServer.getBinding(NamingServer.java:771) [:5.0.5.Final]
    at org.jnp.server.NamingServer.getBinding(NamingServer.java:779) [:5.0.5.Final]
    at org.jnp.server.NamingServer.getObject(NamingServer.java:785) [:5.0.5.Final]
    at org.jnp.server.NamingServer.lookup(NamingServer.java:396) [:5.0.5.Final]
    at org.jnp.server.NamingServer.lookup(NamingServer.java:399) [:5.0.5.Final]
    at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:728) [:5.0.5.Final]
    at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:688) [:5.0.5.Final]
    at javax.naming.InitialContext.lookup(InitialContext.java:411) [:1.7.0_25]
    at org.jnp.interfaces.NamingContext.resolveLink(NamingContext.java:1350) [:5.0.5.Final]
    ... 61 more

另外,在 JBoss 6 启动日志中,我也看到了这个:

 [BeanInstantiatorDeployerBase] Installed org.jboss.ejb3.instantiator.impl.Ejb31SpecBeanInstantiator@fbda95 into MC at org.jboss.ejb.bean.instantiator/mmpl/server/MySingletonClass

然后再往下看一点:

DEPLOYMENTS MISSING DEPENDENCIES:
  Deployment "jboss-switchboard:appName=mmpl,module=server,name=MyMDB" is missing the following dependencies:
   Dependency "jboss.j2ee:ear=mmpl.ear,jar=server.jar,name=MySingletonClass,service=EJB3" (should be in state "Installed", but is actually in state "** NOT FOUND Depends on 'jboss.j2ee:ear=mmpl.ear,jar=server.jar,name=MySingletonClass,service=EJB3',whenRequired=MapControllerStateModel$ControllerStateWrapper@7ba014{Installed},dependentState=MapControllerStateModel$ControllerStateWrapper@7ba014{Installed} **")
  Deployment "jboss.j2ee:ear=mmpl.ear,jar=server.jar,name=MySingletonClass,service=EJB3_endpoint" is missing the following dependencies:
    Dependency "jboss.j2ee:ear=mmpl.ear,jar=server.jar,name=MySingletonClass,service=EJB3" (should be in state "Installed", but is actually in state "** NOT FOUND Depends on 'jboss.j2ee:ear=mmpl.ear,jar=server.jar,name=MySingletonClass,service=EJB3' **")

DEPLOYMENTS IN ERROR:
  Deployment "jboss.j2ee:ear=mmpl.ear,jar=server.jar,name=MySingletonClass,service=EJB3" is in error due to the following reason(s): ** NOT FOUND Depends on 'jboss.j2ee:ear=mmpl.ear,jar=server.jar,name=MySingletonClass,service=EJB3',whenRequired=MapControllerStateModel$ControllerStateWrapper@7ba014{Installed},dependentState=MapControllerStateModel$ControllerStateWrapper@7ba014{Installed} **, ** NOT FOUND Depends on 'jboss.j2ee:ear=mmpl.ear,jar=server.jar,name=MySingletonClass,service=EJB3' **

【问题讨论】:

  • 它以前有效,对吧?如果您评论“阅读器”bean(或 bean 声明本身)的用法,它是否有效?
  • @PiotrNowicki 是的,如果我注释掉 @EJB 行及其用法,就会收到消息。我需要在某处的 XML 中声明我的单例吗?
  • 不,您不需要在 XML 中声明它们。所以没有错误,即使第一条消息没有到达并且在部署期间没有错误?还有一件事——你的 Singleton EJB 正在实现一个接口。这个界面有注解吗?是本地业务 i-face(默认情况下)还是其他?在您的 MDB 中,您使用其实际类类型而不是此接口访问 Singleton,因此您尝试使用 EJB 的无接口视图。你这样声明吗(@LocalBean)?您可以尝试改用它的业务接口吗?
  • @PiotrNowicki 它只实现了interface 来强制在这个和未来的类似类之间执行一些约定。 interface上没有注解。
  • @PiotrNowicki 同样,如果我在 MDB 中使用 interface,它如何知道要填充该 interface 的哪个实现?最终会有几个

标签: java singleton ejb jms message-driven-bean


【解决方案1】:

我最终放弃了 ejb 单例的想法并使用了以下方法,效果很好:

public class MySingletonClass implements SomeInterface
{
   private ScheduledFuture<?> handle;
   private static ScheduledExecutorService executor;
   private static SomeInterface instance;

   public void readResponseMsg(Document responseXml)
   {
       resetTimer();
       //do stuff
   }

   /**
    * Creates the timer.
    */
   private void resetTimer()
   {
      if (handle != null)
      {
         handle.cancel(false);
      }
      handle = executor.schedule(this, 30l, TimeUnit.SECONDS);
   }


   /**
    * Initialize the static variables. Should only be called once per server
    * restart
    */
   private static void init()
   {
      try
      {
         executor = Executors.newSingleThreadScheduledExecutor();
         inited = true;
      }
      catch (Exception e)
      {
         ourLogger.error("Exception initializing ScheduleResultReader", e);
      }
   }

   public static SomeInterface getInstance()
   {
      if (instance == null)
      {
         instance = new MySingletonClass();
      }

      return instance;
   }

}

【讨论】:

    猜你喜欢
    • 2018-11-21
    • 1970-01-01
    • 1970-01-01
    • 2011-09-07
    • 2017-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多