JMS客户端路由

时间:2017-07-24 20:31:40

标签: java jms

大家好我正在开发一个JMS客户端。我理解我的JMS资源是ConnectionFactory,QueueConnectionFactory和Queue / Topic。我使用java开发了这个,它在使用glassfish的本地服务器机器上运行良好。

我想确定我收到的路由点上的Connectionfactory或QueueConnectionFactory是什么。我将如何实现连接到vPN服务器。 CSD端口为7016,CSD IP为10.10.10.76

<routingPoint>
            <name>CSD</name>
            <inboundQueue>interfaceCsdOut</inboundQueue>
            <outboundQueue>interfaceCsdIn</outboundQueue>
               <context>glassfish-csd</context>
            <matchingKey>DESTINATION</matchingKey>
            <controlPoint>SWITCH</controlPoint>
        </routingPoint>

这是我在本地计算机上连接的java代码的片段。

    Properties props = new Properties();
                      props.put(Context.PROVIDER_URL, "mq://127.0.0.1:7676");

                               // Create the initial context for remote JMS server
               InitialContext cntxt = new InitialContext(props);
                      //System.out.println("Context Created");  
  // JNDI Lookup for QueueConnectionFactory in remote JMS Provider
                      QueueConnectionFactory qFactory = (QueueConnectionFactory)cntxt.lookup("TestQueueConnectionFactory");

  // Create a Connection from QueueConnectionFactory
                       Connection connection = qFactory.createConnection();
                          //System.out.println("Connection established with JMS Provider ");

  // Initialise the communication session 
                  Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

  // Create the message
                     TextMessage message = session.createTextMessage();
                       message.setJMSDeliveryMode(DeliveryMode.NON_PERSISTENT);
                        message.setText(finalm);

                          // JNDI Lookup for the Queue in remote JMS Provider
                                 Queue queue = (Queue)cntxt.lookup("jms/Escrow");

                               // Create the MessageProducer for this communication 
                               // Session on the Queue we have
                             MessageProducer mp = session.createProducer(queue);

                                // Send the message to Queue
                                 mp.send(message);
                                 //System.out.println(finalm);

                                 // Make sure all the resources are released 
                                       mp.close();
                                 session.close();
                                cntxt.close();

1 个答案:

答案 0 :(得分:0)

听起来您想要从外部客户端连接到这些队列。

在这种情况下,此客户端需要一些客户端jar,例如JMS Client。您的提供商网址是:

  

IIOP://10.10.10.76:7016

(检查端口以确保它是引导程序端口)

Connectionfactory或QueueConnectionFactory是

  

TestQueueConnectionFactory

你的队列:

  

JMS /中介

如果您使用的是websphere并且具有重要的jms客户端jar,您的客户端代码可能如下所示:(对于Glassfish应该类似)

import java.io.File; 
import java.io.StringWriter; 
import java.util.Hashtable; 

import javax.jms.Connection; 
import javax.jms.ConnectionFactory; 
import javax.jms.JMSException; 
import javax.jms.MessageProducer; 
import javax.jms.Queue; 
import javax.jms.Session; 
import javax.jms.TextMessage; 
import javax.naming.Context; 
import javax.naming.InitialContext; 
import javax.naming.NamingException; 
import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.parsers.ParserConfigurationException; 
import javax.xml.transform.OutputKeys; 
import javax.xml.transform.Transformer; 
import javax.xml.transform.TransformerException; 
import javax.xml.transform.TransformerFactory; 
import javax.xml.transform.dom.DOMSource; 
import javax.xml.transform.stream.StreamResult; 

import org.json.JSONException; 
import org.json.JSONObject; 
import org.w3c.dom.Attr; 
import org.w3c.dom.Document; 
import org.w3c.dom.Element; 

public class SimpleClientLast { 


        public static void main(String args[]) throws JMSException, NamingException { 

                Hashtable env = new Hashtable(); 
                env.put(Context.INITIAL_CONTEXT_FACTORY, 
                                "com.ibm.websphere.naming.WsnInitialContextFactory"); 
                env.put(Context.PROVIDER_URL, "iiop://10.10.10.76:7016"); 

                Context jndiContext = null; 
                try { 
                        jndiContext = new InitialContext(env); 
                } catch (NamingException e) { 

                        System.out.println("Could not create JNDI API context: " 
                                        + e.toString()); 
                        System.exit(1); 
                } 

                ConnectionFactory connectionFactory = (ConnectionFactory) jndiContext 
                                .lookup("TestQueueConnectionFactory"); 
                Connection qConn = connectionFactory.createConnection(); 
                Session qSession = qConn.createSession(false, Session.AUTO_ACKNOWLEDGE); 
                Queue q = (Queue) jndiContext.lookup("jms/Escrow"); 

                System.out.println("before"); 
                MessageProducer producer = qSession.createProducer(q); 
                TextMessage message = qSession.createTextMessage(); 

                System.out.println("JSON begin: "); 
                String jsonText = "{"mytext":"myTextDetails"}" 

                System.out.println(jsonText); 
                message.setText(jsonText); 
                producer.send(message); 

                System.out.println("JSON end."); 

                System.out.println("after"); 

                producer.close(); 
                qSession.close(); 
                qConn.close(); 

        }