JMS&ActiveMQ实战- MDB

2019-03-28 00:33|来源: 网络

在EJB3中,一个MDB(消息驱动Bean)就是一个实现了MessageListener接口的POJO。下面就是一个简单的MDB。

@MessageDriven(activationConfig={

       @ActivationConfigProperty(propertyName="destinationType",

               propertyValue="javax.jms.Queue"),

       @ActivationConfigProperty(propertyName="destination",

               propertyValue="queue/testQueue")})

public class SimpleMDB implements MessageListener {

 

   public void onMessage(Message message) {

       try {

           System.out.println("Receive Message : " + ((TextMessage)message).getText());

       } catch (JMSException e) {

           e.printStackTrace();

       }

   }

}


它要求必须标注为@MessageDriven。它所监听Destination通过标注属性来注入。


下面是一个发送消息的StatelessBean:

@Remote

public interface IMessageSender {

   public void sendMessage(String content) throws Exception;

}


@Stateless

@Remote

public class MessageSender implements IMessageSender {

   @Resource(mappedName="ConnectionFactory")

   private ConnectionFactory factory;

 

   @Resource(mappedName="queue/testQueue")

   private Queue queue;

 

 

   public void sendMessage(String content) throws Exception {

       Connection cn = factory.createConnection();

     

       Session session = cn.createSession(false, Session.AUTO_ACKNOWLEDGE);

       MessageProducer producer = session.createProducer(queue);

       producer.send(session.createTextMessage(content));

   }

}

这个EJB只有一个方法SendMessage。ConnectionFactory和Queue通过标注注入。


接下来是客户端:

public class MessageSenderClient {

   public static void main(String[] args) throws Exception {

       Properties props = new Properties();

       props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");

       props.setProperty(Context.PROVIDER_URL, "localhost:2099");

       Context context = new InitialContext(props);

       IMessageSender messageSender = (IMessageSender) context.lookup("MessageSender/remote");

       messageSender.sendMessage("Hello");

   }

}

它通过JNDI查找到上面的EJB,然后调用sengMessage.



本文链接:JMS&ActiveMQ实战- MDB,领悟书生学习笔记,转自:http://www.360doc.com/content/09/0712/21/18042_4242116.shtml

相关问答

更多
  • 不,没有标准的方法来动态设置激活配置属性。 我甚至从未听说过特定于供应商的方法来动态设置激活配置属性。 我想您可以动态重建EAR并使用特定于供应商的API重新部署它,或者某些应用程序服务器支持动态覆盖该配置。 No, there is no standard way to dynamically set activation config properties. I've never even heard of a vendor-specific way to dynamically set activat ...
  • 根据Spring的问题,它是来自Weblogic服务器的类加载器问题: https : //jira.spring.io/browse/SPR-14187 It´s a classloader issue from Weblogic's server according Spring's issue: https://jira.spring.io/browse/SPR-14187
  • 发现我的错误一个主题听jms / topic / Subscriber第二个jms / topic / Subscribers.improved。 found my mistake one topic listen jms/topic/Subscriber second jms/topic/Subscribers.improved.
  • 终于搞定了。 我完全删除了ejb-jar.xml,并保持sun-ejb-jar.xml不变。 然后我按如下方式注释了类foo.QueueListenerMDB: @MessageDriven(name = "QueueListener", mappedName = "QueueListener") public class QueueListenerMDB implements MessageListener { ... } Got it to work finally. I removed ej ...
  • 使用消息中的“source”消息属性和消息选择器是恕我直言的方法。 现在,如果您不想在MDB中(在注释中)对其进行硬编码,那么请使用部署描述符并在打包时设置消息选择器。 Using a "source" message attribute in the message and a message selector is IMHO the way to go. Now, if you don't want to hard code this in the MDB (in annotations), then ...
  • 是的,您应该能够连接到应用程序服务器的JMS队列。 JMS是一个非常好的标准,因此在获得Connection / Queue之后,实现与供应商无关。 下面简要概述了使用JMS: JMS Sample创建消费者/生产者所需要编写的内容 这将详细介绍WebLogic JMS的每个步骤: 开发WebLogic JMS应用程序 我没有使用WebLogic或它的JMS实现,也许您必须配置Weblogics JMS: 配置和管理WebLogic JMS 有关WebLogic JMS提供程序的编程功能的全面概述,请查看: ...
  • 您需要找到正确的J2CMessageEndpoint并将其停用。 看这里 : 使用wsadmin脚本编制管理消息端点生命周期 J2CMessageEndpoint MBean 虽然第一个链接讨论了wsadmin脚本,但你也可以从java代码中做任何事情。 You need to find the right J2CMessageEndpoint and deactivate it. Look here : Managing the message endpoint lifecycle using wsadm ...
  • 我发现MDB上没有事务上下文! 当我通过调用检查事务ID weblogic.transaction.TxHelper.getTransactionId() 收到null,并在调用messageDrivenContext.getRollbackOnly()时获得异常 java.lang.IllegalStateException: [EJB:010156]Illegal attempt to call EJBContext.getRollbackOnly() from an EJB that was no ...
  • 我认为您可以使用工厂方法来即时化您的MDB bean并使用方法 Object getBean(String name, Object... args) throws BeansException; 您的代码中的ApplicationContext用于以编程方式实例化bean。 据我所知,这个方法允许你将参数路径化为工厂方法。 这里在java doc中为这个方法说了什么: 返回指定bean的实例,该实例可以是共享的或独立的。 允许指定显式构造函数参数/工厂方法参数,覆盖bean定义中指定的缺省参数(如果有) ...
  • 最后我找到了一种方法。 我在这里张贴它可能对某人有所帮助。 如果我总结了我做了什么。 首先,我们需要wildfly中的应用程序用户。 为此,转到{wildflyHome} / bin并运行./add-user.sh 。 将用户类型指定为应用程序(选项b )。 并提供用户名和密码。 供参考: 链接1 在上面的链接中采取步骤1和步骤2。 将standalone-full.xml修改为 ...