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 String property of message
024 *
025 * @author Sandro Leuchter
026 *
027 */
028public class ChatClientUserProperty 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 ChatClientUserProperty(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.getStringProperty("user") + "> " + 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                ChatClientUserProperty node = null;
082                try {
083                        node = new ChatClientUserProperty("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                                TextMessage msg = node.session.createTextMessage(line);
089                                msg.setStringProperty("user", args[0]);
090                                node.producer.send(msg);
091                        }
092                } catch (NamingException | JMSException | IOException e) {
093                        System.err.println(e);
094                } finally {
095                        try {
096                                if ((node != null) && (node.producer != null)) {
097                                        node.producer.close();
098                                }
099                                if ((node != null) && (node.consumer != null)) {
100                                        node.consumer.close();
101                                }
102                                if ((node != null) && (node.session != null)) {
103                                        node.session.close();
104                                }
105                                if ((node != null) && (node.connection != null)) {
106                                        node.connection.close();
107                                }
108                        } catch (JMSException e) {
109                                System.err.println(e);
110                        }
111                }
112        }
113}