0
在這種情況下,我有以下將代碼發送到遠程jms提供程序(Tibco)的代碼,我正在尋找將發送代碼放入Bean管理事務的解決方案, usertransaction,但不知道如何做到這一點。在事務中將消息發佈到遠程TIBCO JMS提供程序
import java.util.Hashtable;
import javax.annotation.Resource;
import javax.inject.Singleton;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.transaction.Transactional;
import javax.transaction.Transactional.TxType;
import javax.transaction.UserTransaction;
@Singleton
public class ServiceLayer implements IServiceLayer{
public final static String JNDI_FACTORY = "com.tibco.tibjms.naming.TibjmsInitialContextFactory";
public final static String PROVIDER_URL = "tcp://serverurl:7225";
public final static String JMS_FACTORY = "XAQueueConnectionFactory";
public final static String QUEUE = "Queue";
@Resource
public UserTransaction utx;
public void sendMessage(String message) throws Exception {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, PROVIDER_URL);
for (int i = 0; i < 1; i++) {
// Define queue
QueueSender qsender = null;
QueueSession qsession = null;
QueueConnection qcon = null;
try {
utx.begin();
InitialContext ctx = new InitialContext(env);
QueueConnectionFactory qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
qcon = qconFactory.createQueueConnection();
qsession = qcon.createQueueSession(true, -1);
Queue queue = (Queue) ctx.lookup(QUEUE);
TextMessage msg = qsession.createTextMessage();
msg.setText(message);
qsender = qsession.createSender(queue);
qsender.send(msg);
System.out.println("sleep 5 secs.." + message.toString());
Thread.sleep(5000);
System.out.println("Message [" + msg.getText() + "] sent to Queue: " + QUEUE);
// qsession.commit();
utx.commit();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (qsender != null)
qsender.close();
if (qsession != null)
qsession.close();
if (qcon != null)
qcon.close();
}
}
}
}
技術堆棧/背景:
Wildfly,CDI,TIBCO EMS 8.2 JMS提供者,Java8,使用獨立-full.xml,用於消費的消息和工程新增genericra.rar資源適配器文件。用於消息消息的消息驅動的Bean。
你在實際的Java EE服務器或只是一個SERVET容器(如Tomcat爲例)運行。前者的答案很容易,後者很難 –
@SteveC我在野蠻容器中使用它,它是j2ee – Zeus
@SteveC我已經用技術堆棧更新了這個問題。請看一下。謝謝。 – Zeus