我们在网络上遇到延迟问题。每个连接、会话和生产者创建都会受到 1 毫秒的惩罚。这就是为什么我想在我的无状态 EJB 实例变量中缓存我的 jms 连接、会话和 MessageProducer。
我可以在@PostConstruct 中创建它们,这样做安全吗?
@Stateless
public class MyEJB {
@Resource(mappedName = Messaging.LOCAL_JNDI_CONN_FACTORY)
private ConnectionFactory connectionFactory;
@Resource(mappedName = AutoRecolteIndexerConsumer.QUEUE_NAME)
private Queue queue;
private Connection connection;
private Session session;
private MessageProducer producer;
@PostConstruct
public void init() {
connection = connectionFactory.createConnection();// 1ms
session = connection.createSession(true, Session.SESSION_TRANSACTED);// 1ms
producer = session.createProducer(queue); //1ms
}
public void send(Object data) {
ObjectMessage obj = session.createObjectMessage();
obj.setObject(data);
producer.send(obj);
}
}