Java JMS 客户端 - 绑定到特定的本地端口

San*_*ian 1 java weblogic jms

我有一个简单的 JMS 客户端 java 代码,它连接到 weblogic 应用程序服务器并将消息发送到定义的 jms 队列。

对于 JMS 客户端与服务器建立的每个连接,在具有 JMS 队列的 Weblogic 应用程序服务器和运行 JMS 客户端代码的机器之间建立一个底层 TCP 连接。

根据我有限的知识,我认为连接中的本地端口是随机选择的。如果这是错误的,请纠正我。

在我们的生产服务器中,由于一些严格的客户政策,我们被要求将本地端口限制为固定端口范围,而不是任何随机端口。是否可以通过在 JMS 客户端代码中指定任何连接工厂属性来做同样的事情。

E.g. 
192.19.81.223 -> m/c where Weblogic is installed
7001 -> Weblogic Server admin port, where the JMS Server is targeted
192.19.105.54 -> m/c where the JMS Client code is running
61372 -> Random port being selected in the m/c where JMS Client is run.

$home > netstat -an|grep 7001
tcp        0      0 ::ffff:192.19.81.223:7001   :::*                             LISTEN
tcp        0      0 ::ffff:192.19.81.223:7001   ::ffff:192.19.105.54:61372     ESTABLISHED
Run Code Online (Sandbox Code Playgroud)

每次运行客户端代码时,随机端口都会从 61372 更改为 59874、56987 等……我无法修复此端口号。到一个确定的值或一个值的范围。

请让我知道这是否可以完成。

示例 JMS 客户端代码:

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

import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.naming.directory.InitialDirContext;

import org.apache.commons.io.FileUtils;

public class JmsSender
{
    private static InitialContext ctx = null;
    private static ConnectionFactory connFactory = null;

    public static void main(String[] args) throws Exception
    {
    initializeContext();
    createConnectionFactory();
    sendToQueue();
    }


    private static void sendToQueue() {
    QueueSession queueSession = null;
    try
    {
        Queue queue = getQueue();
        queueSession = getQueueSession();
        MessageProducer producer = queueSession.createSender(queue);
        createMessagesAndSend(queueSession, producer);
    }
    catch (Exception e){
        e.printStackTrace();
    }
    finally
    {
        try {
        if(queueSession != null)
            queueSession.close();
        } catch (JMSException e) {
        e.printStackTrace();
        }
    }
    }

    private static void createMessagesAndSend(Session session, MessageProducer producer) throws IOException, JMSException {
    String buffer = FileUtils.readFileToString(new File("D:\\Testdata\\SAMPLE.txt"));
    TextMessage message = session.createTextMessage(buffer);
    producer.send(message);
    }


    private static QueueSession getQueueSession() throws NamingException, JMSException {
    QueueConnection queueConn = ((QueueConnectionFactory)connFactory).createQueueConnection();
    QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
    return queueSession;
    }

    private static void createConnectionFactory() throws NamingException {
    connFactory = (ConnectionFactory) ctx.lookup("jms/ConnectionFactory");
    }

    private static void initializeContext() throws NamingException {
    ctx = new InitialDirContext(getProperties());
    }

    private static Hashtable<String,String> getProperties() {
    Hashtable<String,String> environment = new Hashtable<String,String>();
    environment.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
    environment.put(Context.PROVIDER_URL, "t3://192.19.81.223:7001");
    environment.put(Context.REFERRAL, "throw");
    return environment;
    }

    private static Queue getQueue() throws NamingException {
    return (Queue) ctx.lookup("QueueIncoming");
    }
}
Run Code Online (Sandbox Code Playgroud)

use*_*421 5

我认为连接中的本地端口是随机选择的。如果这是错误的,请纠正我。

你是对的。

在我们的生产服务器中,由于一些严格的客户政策,我们被要求将本地端口限制为固定端口范围,而不是任何随机端口。

您的客户被误导或不明智或服务不当。像这样的防火墙规则没有任何好处,并且在大多数情况下无法在应用程序中实现。

是否可以通过在 JMS 客户端代码中指定任何连接工厂属性来做同样的事情。

不是我所知道的。您需要向您的客户介绍出站防火墙规则。它们没有任何有用的安全性或其他目的。如果他不这么认为,问他什么,以及如何。