2014-05-14 54 views
4

我想向EMS主題發佈測試消息,並可以使用某些說明。到目前爲止,我已設法做到這一點如何使用Java將消息發佈到EMS主題

import com.tibco.tibjms.TibjmsConnectionFactory; 
import com.tibco.tibjms.TibjmsTopicConnectionFactory; 

public class Connect { 
    public static void main(String[] args) { 

     String url = "tcp://host:6600"; 
     TibjmsConnectionFactory cf = new TibjmsTopicConnectionFactory(url); 

     cf.setUserName("user1"); 
     cf.setUserPassword(""); 
     System.out.println(cf); 
    } 
} 

它產生以下。如何發佈消息到主題「TOPIC1」或隊列「Q1」

TopicConnectionFactory[URL=tcp://localhost:6600;clientID=null;Properties={com.tibco.tibjms.factory.password=, com.tibco.tibjms.factory.username=user1}] 

回答

7

我通過修改我的EMS 8.0「示例」文件夾中的「tibjmsMsgProducer.java」創建了以下代碼。查看此文件夾中的所有Java示例以供進一步參考。

此代碼使用默認用戶和密碼向本地EMS發佈簡單的硬編碼文本消息。目標Topic是「topic1」(最後一行)。

import javax.jms.Connection; 
import javax.jms.ConnectionFactory; 
import javax.jms.Destination; 
import javax.jms.JMSException; 
import javax.jms.MessageProducer; 
import javax.jms.Session; 
import javax.jms.TextMessage; 

public class tibjmsMsgTopicProducer { 

static String serverUrl = "localhost"; 
static String userName = "admin"; 
static String password = "admin"; 

public static void sendTopicMessage(String topicName, String messageStr) { 

    Connection connection = null; 
    Session session = null; 
    MessageProducer msgProducer = null; 
    Destination destination = null; 

    try { 
     TextMessage msg; 

     System.out.println("Publishing to destination '" + topicName 
       + "'\n"); 

     ConnectionFactory factory = new com.tibco.tibjms.TibjmsConnectionFactory(
       serverUrl); 

     connection = factory.createConnection(userName, password); 

     /* create the session */ 
     session = connection 
       .createSession(javax.jms.Session.AUTO_ACKNOWLEDGE); 

     /* create the destination */ 
     destination = session.createTopic(topicName); 

     /* create the producer */ 
     msgProducer = session.createProducer(null); 

     /* publish messages */ 
     /* create text message */ 
     msg = session.createTextMessage(); 

     /* set message text */ 
     msg.setText(messageStr); 

     /* publish message */ 
     msgProducer.send(destination, msg); 

     System.out.println("Published message: " + messageStr); 

     /* close the connection */ 
     connection.close(); 

    } catch (JMSException e) { 
     e.printStackTrace(); 
    } 
} 

/*----------------------------------------------------------------------- 
* main 
*----------------------------------------------------------------------*/ 
public static void main(String[] args) { 
    tibjmsMsgTopicProducer.sendTopicMessage("topic1", 
      "This is the message content !"); 
} 

}

注意:您還可以想用EMS with Spring-JMS一個更 「企業級」 的解決方案。上面的代碼要簡單得多。

注2:我使方法「靜態」。這僅用於演示目的。 JMS中的連接成本很高,所以我們通常會嘗試重用它們。查看所有TIBCO提供的更好設置Java類的示例。如果可以的話,實例化和重用連接。 此外,J2EE或Spring解決方案將支持內置的連接池。

+0

工作很好。任何想法如何設置消息頭的屬性? – Bala

+1

類似於:msg.setStringProperty(「PROPNAME」,「PROP_VALUE」); – GhislainCote

1

我沒碰過EMS一段時間 - 但基本上EMS不過是一個JMS實現。所有實現特定的東西已經隱藏給你。您只需使用標準JMS方式發佈/訂閱主題,您可以在Java教程和在線資源中找到很好的示例。我會在這裏保存我的醜陋示例代碼:-)

+0

同意@Alex Suo。一些示例代碼在這裏。 http://www.novell.com/documentation/extend52/Docs/help/MP/jms/tutorial/pubSub-1.htm – Priyesh

0

你可以看看this test project @gelnyang建。而this是專門發佈EMS消息的類。在該項目下,您還可以找到其他EMS相關功能。

相關問題