001package var.mom.jms.chat;
002
003import java.io.BufferedReader;
004import java.io.IOException;
005import java.io.InputStreamReader;
006
007import javax.jms.Connection;
008import javax.jms.ConnectionFactory;
009import javax.jms.Destination;
010import javax.jms.JMSException;
011import javax.jms.Message;
012import javax.jms.MessageConsumer;
013import javax.jms.MessageListener;
014import javax.jms.MessageProducer;
015import javax.jms.Session;
016import javax.jms.TextMessage;
017import javax.naming.Context;
018import javax.naming.InitialContext;
019import javax.naming.NamingException;
020
021/**
022 * Chat Client using publish/subscribe on JMS provider Topic; represents user
023 * name as part of message payload
024 *
025 * @author Sandro Leuchter
026 *
027 */
028public class ChatClient implements MessageListener {
029        private Connection connection;
030        private Session session;
031        private MessageProducer producer;
032        private MessageConsumer consumer;
033
034        /**
035         * constructor, establishes and starts connection to JMS provider specified in
036         * JNDI (via jndi.properties), afterwards producer and consumer are ready
037         *
038         * @param sendDest    Destination for producer
039         * @param receiveDest Destination for consumer
040         *
041         * @throws NamingException JNDI exceptions
042         * @throws JMSException    JMS exceptions
043         * @see javax.jms.Destination
044         */
045        public ChatClient(String sendDest, String receiveDest) throws NamingException, JMSException {
046                Context ctx = new InitialContext();
047                ConnectionFactory factory = (ConnectionFactory) ctx.lookup("ConnectionFactory");
048                this.connection = factory.createConnection();
049                this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
050                Destination destOut = (Destination) ctx.lookup(sendDest);
051                Destination destIn = (Destination) ctx.lookup(receiveDest);
052                this.producer = this.session.createProducer(destOut);
053                this.consumer = this.session.createConsumer(destIn);
054                this.consumer.setMessageListener(this);
055                this.connection.start();
056        }
057
058        /**
059         * asynchronous message consumption
060         *
061         * @see javax.jms.MessageListener
062         */
063        @Override
064        public void onMessage(Message message) {
065                if (message instanceof TextMessage) {
066                        TextMessage textMessage = (TextMessage) message;
067                        try {
068                                System.out.println(textMessage.getText());
069                        } catch (JMSException e) {
070                                System.err.println(e);
071                        }
072                }
073        }
074
075        /**
076         * main routine and starting point of program
077         *
078         * @param args[0] user name
079         */
080        public static void main(String[] args) {
081                ChatClient node = null;
082                try {
083                        node = new ChatClient("var.mom.jms.channel1", "var.mom.jms.channel1");
084                        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
085                        String line;
086                        while (true) {
087                                line = input.readLine();
088                                node.producer.send(node.session.createTextMessage(args[0] + "> " + line));
089                        }
090                } catch (NamingException | JMSException | IOException e) {
091                        System.err.println(e);
092                } finally {
093                        try {
094                                if ((node != null) && (node.producer != null)) {
095                                        node.producer.close();
096                                }
097                                if ((node != null) && (node.consumer != null)) {
098                                        node.consumer.close();
099                                }
100                                if ((node != null) && (node.session != null)) {
101                                        node.session.close();
102                                }
103                                if ((node != null) && (node.connection != null)) {
104                                        node.connection.close();
105                                }
106                        } catch (JMSException e) {
107                                System.err.println(e);
108                        }
109                }
110        }
111}