2017-03-01 95 views
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。

+0

你在實際的Java EE服務器或只是一個SERVET容器(如Tomcat爲例)運行。前者的答案很容易,後者很難 –

+0

@SteveC我在野蠻容器中使用它,它是j2ee – Zeus

+0

@SteveC我已經用技術堆棧更新了這個問題。請看一下。謝謝。 – Zeus

回答

1

我找到了解決方案。幸運的是,我已經在wildfly上安裝了genericra.rar。所以,這對我來說是一塊蛋糕。 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資源適配器設置。大多數步驟都是準確的: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> 

感謝賈斯汀幫我完成這個:https://developer.jboss.org/thread/274204

相關問題