[英]Publish messages to remote TIBCO JMS provider in transaction
I have the following code which sends messages to a remote jms provider(Tibco) in this case, I am looking for a solution to put the sending code into a Bean managed transaction or the usertransaction, but not sure how to do that. 在这种情况下,我有以下代码将消息发送到远程jms provider(Tibco),我正在寻找一种解决方案,将发送代码放入Bean管理的事务或usertransaction中,但不确定如何执行。
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();
}
}
}
}
Tech stack/background: 技术栈/背景:
Wildfly, CDI, Tibco ems 8.2 JMS provider, Java8, Using Standalone-full.xml, Added genericra.rar resource adapter file for consuming messages and that works. Wildfly,CDI,Tibco ems 8.2 JMS提供程序,Java8,使用Standalone-full.xml,添加了genericra.rar资源适配器文件以使用消息,并且可以使用。 Message driven beans for consuming messages.
消息驱动的Bean,用于消耗消息。
I found the solution. 我找到了解决方案。 luckily I already had the genericra.rar installed on the wildfly.
幸运的是,我已经在wildfly上安装了genericra.rar。 So, this was piece of cake for me.
所以,这对我来说是小菜一碟。 GenericJmsXA jndi name is on the Resource adapter settings.
GenericJmsXA jndi名称位于资源适配器设置上。
@Singleton
@Transactional(value = TxType.REQUIRES_NEW)
public class ServiceLayer implements IServiceLayer {
@Resource(mappedName="GenericJmsXA")
public ConnectionFactory queueFactory;
private String QUEUE = "Queue";
public void sendMessage(String message) throws Exception {
MessageProducer qsender = null;
Session qsession = null;
Connection qcon = null;
try{
qcon = this.queueFactory.createConnection();
qsession = qcon.createSession(true, QueueSession.AUTO_ACKNOWLEDGE);
Queue q = qsession.createQueue(QUEUE);
qsender = qsession.createProducer(q);
TextMessage tm = qsession.createTextMessage(message);
System.out.println("Before sending to Queue: Waiting for 5 seconds. " + QUEUE);
qsender.send(tm);
Thread.sleep(5000);
System.out.println("Message [" + tm.getText() + "] sent to Queue: " + QUEUE);
}catch(Exception e){
System.out.println("Exception : "+e.getMessage());
e.printStackTrace();
}
}
Wildfly resource adapter settings. Wildfly资源适配器设置。 Most of the steps are accurate here : https://github.com/jms-ra/generic-jms-ra just follow them and you will be able to get this to work.
大多数步骤在这里都是正确的: https : //github.com/jms-ra/generic-jms-ra只要遵循它们,您便可以使它起作用。
<subsystem xmlns="urn:jboss:domain:resource-adapters:4.0">
<resource-adapters>
<resource-adapter>
<archive>
generic-jms-ra.rar
</archive>
<transaction-support>NoTransaction</transaction-support>
<connection-definitions>
<connection-definition class-name="org.jboss.resource.adapter.jms.JmsManagedConnectionFactory" jndi-name="java:/GenericJmsXA" enabled="true" use-java-context="true" pool-name="GenericJmsXA" use-ccm="true">
<config-property name="JndiParameters">
java.naming.factory.initial=com.tibco.tibjms.naming.TibjmsInitialContextFactory;java.naming.provider.url=tcp://serverurl:7225
</config-property>
<config-property name="ConnectionFactory">
XAQueueConnectionFactory
</config-property>
<pool>
<min-pool-size>0</min-pool-size>
<max-pool-size>10</max-pool-size>
<prefill>false</prefill>
<use-strict-min>false</use-strict-min>
<flush-strategy>FailingConnectionOnly</flush-strategy>
</pool>
<security>
<application></application>
</security>
</connection-definition>
</connection-definitions>
</resource-adapter>
</resource-adapters>
</subsystem>
Thanks Justin for helping me out on this : https://developer.jboss.org/thread/274204 感谢Justin在这方面帮助我: https : //developer.jboss.org/thread/274204
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.