Biblioteca Java - Rev 31

Subversion Repositories:
Rev:
package test.clientserver;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.util.Random;

public class Client extends Thread implements MessageListener {
    private static int ackMode;
    private String clientQueueName;

    private String clientName;

    private boolean transacted = false;
    private MessageProducer producer;
    private Session session;
    private Destination tempDest;

    static {
        ackMode = Session.AUTO_ACKNOWLEDGE;
    }

    public Client(String clientName) {
        this.clientName = clientName;
        this.clientQueueName = clientName;
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
        Connection connection;
        try {
            connection = connectionFactory.createConnection();
            connection.start();
            session = connection.createSession(transacted, ackMode);
            Destination adminQueue = session.createQueue(clientQueueName);

            //Setup a message producer to send message to the queue the server is consuming from
            this.producer = session.createProducer(adminQueue);
            this.producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

            //Create a temporary queue that this client will listen for responses on then create a consumer
            //that consumes message from this temporary queue...for a real application a client should reuse
            //the same temp queue for each message to the server...one temp queue per client
            tempDest = session.createTemporaryQueue();
            MessageConsumer responseConsumer = session.createConsumer(tempDest);

            //This class will handle the messages to the temp queue as well
            responseConsumer.setMessageListener(this);


        } catch (JMSException e) {
            //Handle the exception appropriately
            e.printStackTrace();
        }
    }

    public void sendMessage() {
        try {
            //Now create the actual message you want to send
            TextMessage txtMessage = session.createTextMessage();
            txtMessage.setText("MyProtocolMessage");

            //Set the reply to field to the temp queue you created above, this is the queue the server
            //will respond to
            txtMessage.setJMSReplyTo(tempDest);

            //Set a correlation ID so when you get a response you know which sent message the response is for
            //If there is never more than one outstanding message to the server then the
            //same correlation ID can be used for all the messages...if there is more than one outstanding
            //message to the server you would presumably want to associate the correlation ID with this
            //message somehow...a Map works good
            String correlationId = this.createRandomString();
            txtMessage.setJMSCorrelationID(correlationId);
            System.out.println(":::" + txtMessage);
            this.producer.send(txtMessage);
        }catch(JMSException e){
            e.printStackTrace();
        }
    }


    private String createRandomString() {
        Random random = new Random(System.currentTimeMillis());
        long randomLong = random.nextLong();
        return Long.toHexString(randomLong);
    }

    public void onMessage(Message message) {
        String messageText = null;
        try {
            if (message instanceof TextMessage) {
                TextMessage textMessage = (TextMessage) message;
                messageText = textMessage.getText();
                System.out.println("messageText = " + messageText + " with id "+message.getJMSMessageID());
            }
        } catch (JMSException e) {
            e.printStackTrace();
            //Handle the exception appropriately
        }
    }

    public void run(){
        while(true) {
            this.sendMessage();
            try {
                Thread.sleep(1000);//one message/second
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {

        Client c1 = new Client("client:echo");
        c1.start();

        Client c2 = new Client("client:dummy");
        c2.start();

        //Client c2 = new Client("client2:echo");
        //c2.start();
    }
}