001package var.mom.jms.revers;
002
003import java.util.HashMap;
004
005import javax.jms.Connection;
006import javax.jms.ConnectionFactory;
007import javax.jms.Destination;
008import javax.jms.JMSException;
009import javax.jms.Message;
010import javax.jms.MessageConsumer;
011import javax.jms.MessageListener;
012import javax.jms.MessageProducer;
013import javax.jms.Queue;
014import javax.jms.Session;
015import javax.jms.TextMessage;
016import javax.naming.Context;
017import javax.naming.InitialContext;
018import javax.naming.NamingException;
019
020/**
021 * replier for String reverter service, reuses MessageProducer objects for
022 * temporary reply queues to requesters via replyProducers
023 *
024 * @author Sandro Leuchter
025 *
026 */
027public class UmdreherReuseProducers implements MessageListener {
028        private Connection connection;
029        private Session session;
030        private MessageConsumer consumer;
031        private HashMap<Destination, MessageProducer> replyProducers = new HashMap<>();
032
033        /**
034         * constructor, establishes and starts connection to JMS provider specified in
035         * JNDI (via jndi.properties), afterwards a consumer is ready
036         *
037         * @throws NamingException JNDI exceptions
038         * @throws JMSException    JMS exceptions
039         */
040        public UmdreherReuseProducers() throws NamingException, JMSException {
041                Context ctx = new InitialContext();
042                ConnectionFactory factory = (ConnectionFactory) ctx.lookup("ConnectionFactory");
043                Queue queue = (Queue) ctx.lookup(Conf.QUEUE);
044                this.connection = factory.createConnection();
045                this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
046                this.consumer = this.session.createConsumer(queue);
047                this.consumer.setMessageListener(this);
048                this.connection.start();
049        }
050
051        /**
052         * actual implementation of reverse service
053         *
054         * @param in String to reverse
055         * @return reverse of in
056         */
057        public String revers(String in) {
058                String out = "";
059                for (char c : in.toCharArray()) {
060                        out = c + out;
061                }
062                return out;
063        }
064
065        /**
066         * asynchronous message consumption
067         *
068         * @see javax.jms.MessageListener
069         */
070        @Override
071        public void onMessage(Message request) {
072                try {
073                        if (request instanceof TextMessage) {
074                                TextMessage requestText = (TextMessage) request;
075                                System.out.println("empfangen: " + requestText.getText());
076                                Destination replyQueue = request.getJMSReplyTo();
077                                MessageProducer replyProducer = null;
078                                if (this.replyProducers.containsKey(replyQueue)) {
079                                        replyProducer = this.replyProducers.get(replyQueue);
080                                        System.out.println("Producer wird wiederverwendet: " + replyQueue.toString());
081                                } else {
082                                        replyProducer = this.session.createProducer(replyQueue);
083                                        this.replyProducers.put(replyQueue, replyProducer);
084                                        System.out.println("Producer wird neu erzeugt: " + replyQueue.toString());
085                                }
086                                TextMessage reply = this.session.createTextMessage();
087                                reply.setText(revers(requestText.getText()));
088                                replyProducer.send(reply);
089                                Thread.sleep(1000);
090                        }
091                } catch (JMSException | InterruptedException e) {
092                        System.err.println(e);
093                }
094
095        }
096
097        /**
098         * main routine and starting point of program
099         *
100         * @param args not used
101         */
102        public static void main(String[] args) {
103                UmdreherReuseProducers node = null;
104                try {
105                        node = new UmdreherReuseProducers();
106                        while (true) {
107                                Thread.sleep(1000);
108                        }
109                } catch (InterruptedException | NamingException | JMSException e) {
110                        System.err.println(e);
111                } finally {
112                        try {
113                                if ((node != null) && (node.consumer != null)) {
114                                        node.consumer.close();
115                                }
116                                if ((node != null) && (node.session != null)) {
117                                        node.session.close();
118                                }
119                                if ((node != null) && (node.connection != null)) {
120                                        node.connection.close();
121                                }
122                        } catch (JMSException e) {
123                                System.err.println(e);
124                        }
125                }
126        }
127}