我需要编写一个要部署到 WLS 10.3.3 的无状态会话 bean。会话 bean 应该能够将文本消息发送到通过 WLS 控制台创建的已知 JMS 队列。因此我编写了以下代码:
package com.mycompany.ejb;
import javax.annotation.Resource;
import javax.ejb.*;
import javax.jms.*;
@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
public class MyEjb{
@Resource(name = "jmsConnectionFactory")
private ConnectionFactory jmsConnectionFactory;
@Resource(name = "queue1")
private Destination queue1;
public MyEjb(){}
public void sendMsgToQueue(String payload, ConnectionFactory connFactory, Destination destination) throws Exception{
if(payload == null)
throw new IllegalArgumentException("Message payload is null");
if(connFactory == null)
throw new IllegalArgumentException("Connection factory is null");
if(destination == null)
throw new IllegalArgumentException("Message destination is null");
Connection connection = connFactory.createConnection();
Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
MessageProducer messageProducer = session.createProducer(destination);
TextMessage textMessage = session.createTextMessage();
textMessage.setText(payload);
messageProducer.send(textMessage);
}
}
我现在需要做的是提供一个有效的 weblogic-ejb-jar.xml 以及一个资源引用到 jndi-name 的映射。有人可以提供一个带有以下映射的 weblogic-ejb-jar.xml 示例:
- jmsConnectionFactory 应该绑定到 jndi-name com.mycompany.jmsXAConnFactory 的连接工厂
- queue1 应该绑定到 jndi-name com.mycompany.jmsQueue1 的队列