1
votes

I have a simple JMS Client java code which connects to a weblogic application server and sends messages to the jms queue defined.

For each connection made by the JMS Client to the server there is an underlying TCP connection being made between the Weblogic application server which has the JMS Queue and the machine where the JMS Client code is been run.

As per my limited knowledge, 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.

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

Every time i run the client code, the random port keeps changing from 61372 to 59874, 56987 etc... I'm not able to fix this port no. to a definite value or a range of values.

Please let me know if this can be done.

Sample JMS Client Code:

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");
    }
}
1

1 Answers

5
votes

I think the local port in the connection is randomly chosen. Please correct me if this is wrong.

You are correct.

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.

Your customer is misinformed or ill-advised or ill-served. There is no benefit to firewall rules like this, and it isn't implementable in applications in most cases.

Is it possible to do the same by specifying any connection factory attributes in the JMS Client code.

Not that I am aware of. You need to educate your client about outbound firewall rules. They serve no useful security or other purpose. If he thinks otherwise ask him what, and how.