activemq-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From smt <smakhn...@yandex.ru>
Subject No way to use activeMQ via http. TCP works fine, but not http. Please help.
Date Mon, 04 May 2009 08:43:08 GMT

I developed simplest application where
client sends array of Integers as ObjectMessage to Server.
Server application calculates sum of this values and return the result as
ObjectMessage back to Client.
Other words, it is simplest example ever.

When I use tcp protocol (tcp://localhost:61616) everything works fine.

However after setting http

(just added
<transportConnector name="http" uri="http://localhost:8080"/>
to activemq.xml)

I see really bad performance.
Sometimes Server handle 20 clients and blocked, sometime it's able to server
50 clients then blocked.
If I stop client application, then start it again there can be delay of 20
seconds!!! until it create the jms connection.
I face some IOException like SocketTimeOutException.

But with tcp EVERYTHING WORKS fine even with thousands of clients.
Unfortunately we may not use tcp cause we have to support
proxy servers and http tunneling in the production.

Below I attach maven project for the issue. It contains 3 files.
ServerThread.java, ClientThread.java and Constants.java
It's enough to change BROKER_URL val in Constants.java to switch http and
tcp mode communication.

1) ServerThread.java

public class ServerThread extends Thread {
    private MessageProducer replyProducer;

    public ServerThread() throws JMSException {
        ConnectionFactory factory = new
ActiveMQConnectionFactory(Constants.BROKER_URL);
        Connection jmsConnection = factory.createConnection();
        jmsConnection.start();
        QueueSession session = ((QueueConnection)
jmsConnection).createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        Destination adminQueue = session.createQueue(Constants.QUEUE_NAME);
        replyProducer = session.createProducer(null);
        replyProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        MessageConsumer consumer = session.createConsumer(adminQueue);
        consumer.setMessageListener(new MessageListener() {
            public void onMessage(Message message) {
                new Thread(new OnMessageRunnable(message)).start();
            }
        });
    }

    @Override
    public void run() {

    }

    private class OnMessageRunnable implements Runnable {
        private Message message;

        public OnMessageRunnable(Message message) {
            this.message = message;
        }

        public void run() {
            try {
                ObjectMessage incomingMessage = (ObjectMessage) message;
                Integer[] vals = (Integer[]) incomingMessage.getObject();
                Integer result = 0;
                for (Integer val : vals) {
                    result += val;
                }

                Destination outgoingDestination = message.getJMSReplyTo();

                ObjectMessage outgoingMessage = new ActiveMQObjectMessage();
                outgoingMessage.setObject(result);

                replyProducer.send(outgoingDestination, outgoingMessage);
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        try {

            new ServerThread().start();
        } catch (JMSException e) {
            e.printStackTrace();  //To change body of catch statement use
File | Settings | File Templates.
        }
    }

}

2) ClientThread.java

public class ClientThread extends Thread {
    private QueueSession queueSession;
    private static Object LOCK_OBJECT = new Object();
    private static Integer finished = 0;


    public ClientThread() throws JMSException {
        ConnectionFactory factory = new
ActiveMQConnectionFactory(Constants.BROKER_URL);
        Connection jmsConnection = factory.createConnection();
        jmsConnection.start();
        System.out.println("Connection started");
        queueSession = ((QueueConnection)
jmsConnection).createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    }

    @Override
    public void run() {
        try {
            Queue outgoingQueue =
queueSession.createQueue(Constants.QUEUE_NAME);
            ObjectMessage outgoingMessage = new ActiveMQObjectMessage();

            TemporaryQueue incomingQueue =
queueSession.createTemporaryQueue();
            MessageConsumer consumer =
queueSession.createConsumer(incomingQueue);

            outgoingMessage.setJMSReplyTo(incomingQueue);
            outgoingMessage.setObject(new Integer[]{1, 2, 3});
            MessageProducer producer =
queueSession.createProducer(outgoingQueue);
            producer.send(outgoingQueue, outgoingMessage);

            Message message = consumer.receive();
            ObjectMessage objectMessage = (ObjectMessage) message;
            System.out.println("" + objectMessage.getObject());
            incFinishedCount();
        } catch (JMSException e) {
            e.printStackTrace();  //To change body of catch statement use
File | Settings | File Templates.
        }
    }

    private static void incFinishedCount(){
        synchronized (LOCK_OBJECT){
            ++finished;
        }
    }

    public static void main(String[] args) {
        try {
            System.out.println("Connection started");
            int clientsCount = 100;
            for (int i = 0; i < clientsCount; i++) {
                new ClientThread().start();
            }
            while (clientsCount > finished ){
                //do nothing
            }
            System.out.println("Finished");
        } catch (JMSException e) {
            e.printStackTrace();  //To change body of catch statement use
File | Settings | File Templates.
        }
    }
}

3) Constants.java



public interface Constants {
    String QUEUE_NAME = "QUEUE";

    // to check tcp
    String BROKER_URL = "tcp://localhost:61616";

    // to check http
    //String BROKER_URL = "http://localhost:8080";
}


Attachment maven project 
http://www.nabble.com/file/p23364910/task_jms_issue.zip task_jms_issue.zip 


I'll be really thankful for your help in this issue.
Thank you, guys.
-- 
View this message in context: http://www.nabble.com/No-way-to-use-activeMQ-via-http.-TCP-works-fine%2C-but-not-http.-Please-help.-tp23364910p23364910.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.


Mime
View raw message