2009-08-25 56 views

回答

5

您應該能夠創建一個使用Geronimo中的JNDI服務器的InitialContext。然後,您可以使用它來查找JMS連接工廠和隊列。

以下示例已從http://forums.sun.com/thread.jspa?threadID=5283256改編爲使用Geronimo JNDI Factory。

Context     jndiContext = null; 
ConnectionFactory connectionFactory = null; 
Connection    connection = null; 
Session     session = null; 
Queue     queue = null; 
MessageProducer  messageProducer = null; 

try 
{ 
    //[1] Create a JNDI API InitialContext object. 
    Hashtable properties = new Hashtable(2); 

    // CHANGE these to match Geronimos JNDI service 

    properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.RemoteInitialContextFactory"); 
    properties.put(Context.PROVIDER_URL, "ejbd://127.0.0.1:4201"); 
    jndiContext = new InitialContext(properties); 

    //[2] Look up connection factory and queue. 
    connectionFactory = (ConnectionFactory)jndiContext.lookup("jms/ConnectionFactory"); 
    queue = (Queue)jndiContext.lookup("jms/Queue"); 

    //[3] 
    // - Create connection 
    // - Create session from connection; false means session is not transacted. 
    // - Create sender and text message. 
    // - Send messages, varying text slightly. 
    connection = connectionFactory.createConnection(); 
    session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 
    messageProducer = session.createProducer(queue); 

    //send a message 
    TextMessage message = session.createTextMessage(this.jTextSend.getText()); 
    messageProducer.send(message); 

    //example for send some object 
    //ObjectMessage message = session.createObjectMessage(); 
    //MyObj myObj = new MyObj ("Name"); //this class must be serializable 
    //message.setObject(myObj); 
    //messageProducer.send(message); 
} 
catch(Exception ex) 
{ 
    LOG.error(ex); 
} 
finally 
{ 
    if(connection !=null) 
    { 
     try 
     { 
      connection.close(); 
     } 
     catch(JMSException e) 
     { 
       LOG.error(e); 
     } 
    } 
} 
+0

您可能想補充一點,假設您已經運行了本地JNDI。 – Robin 2009-08-25 18:54:59

0

您可以將在JMS隊列中的消息沒有應用服務器。

但是,您將需要知道如何直接訪問JMS提供程序 - 無需使用JNDI,因爲它是由JavaEE應用程序服務器提供的。

0

你可以做到這一點,並有可能因瘦客戶機正在訪問隊列的多種方式。 @pjp給出的例子可以提供你正確的jar文件來訪問相關的服務器,包括一個jar,它將爲你的應用程序提供一個JNDI實例。這些罐子應由供應商提供,並且可能包含有關如何在不使用JNDI的情況下進行連接的說明。儘管我認爲JNDI方法是最簡單的,並且在服務器上和服務器上保持編碼一致。在IBM的情況下,對於內部JMS提供者和WebSphere MQ(因爲它們是兩種不同的實現),每個供應商都會有不同的jar來提供客戶端訪問。

相關問題