Java JMS 客户端 - 绑定到特定的本地端口
Java JMS Client - Binding to a specific Local Port
我有一个简单的 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
每次我 运行 客户端代码,随机端口不断从 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");
}
}
I think the local port in the connection is randomly chosen. Please correct me if this is wrong.
你是对的。
In our production server, due to some strict customer policy we have been asked to restrict the local port to a fixed port range instead of any random port.
您的客户被误导、不明智或服务不周。像这样的防火墙规则没有任何好处,并且在大多数情况下无法在应用程序中实施。
Is it possible to do the same by specifying any connection factory attributes in the JMS Client code.
我不知道。您需要让您的客户了解出站防火墙规则。它们没有任何有用的安全或其他目的。如果他不这么认为,问他什么,以及如何。
我有一个简单的 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
每次我 运行 客户端代码,随机端口不断从 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");
}
}
I think the local port in the connection is randomly chosen. Please correct me if this is wrong.
你是对的。
In our production server, due to some strict customer policy we have been asked to restrict the local port to a fixed port range instead of any random port.
您的客户被误导、不明智或服务不周。像这样的防火墙规则没有任何好处,并且在大多数情况下无法在应用程序中实施。
Is it possible to do the same by specifying any connection factory attributes in the JMS Client code.
我不知道。您需要让您的客户了解出站防火墙规则。它们没有任何有用的安全或其他目的。如果他不这么认为,问他什么,以及如何。