001package var.mom.jms.tasks;
002
003import javax.jms.Connection;
004import javax.jms.ConnectionFactory;
005import javax.jms.JMSException;
006import javax.jms.Message;
007import javax.jms.MessageConsumer;
008import javax.jms.MessageListener;
009import javax.jms.MessageProducer;
010import javax.jms.Queue;
011import javax.jms.Session;
012import javax.jms.TextMessage;
013import javax.naming.Context;
014import javax.naming.InitialContext;
015import javax.naming.NamingException;
016
017public class Umdreher implements MessageListener {
018        private Connection connection;
019        private Session session;
020        private MessageConsumer consumer;
021
022        public Umdreher() throws NamingException, JMSException {
023                Context ctx = new InitialContext();
024                ConnectionFactory factory = (ConnectionFactory) ctx.lookup("ConnectionFactory");
025                Queue queue = (Queue) ctx.lookup(Conf.QUEUE);
026                this.connection = factory.createConnection();
027                this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
028                this.consumer = this.session.createConsumer(queue);
029                this.consumer.setMessageListener(this);
030                this.connection.start();
031        }
032
033        @Override
034        public void onMessage(Message request) {
035                try {
036                        if (request instanceof TextMessage) {
037                                TextMessage requestText = (TextMessage) request;
038                                System.out.println("empfangen: " + requestText.getText());
039                                MessageProducer replyProducer = this.session.createProducer(request.getJMSReplyTo());
040                                TextMessage reply = this.session.createTextMessage();
041                                /**
042                                 * modifying
043                                 */
044                                String answertext = revers(requestText.getText());
045                                reply.setText("echo: " + answertext);
046
047                                // reply.setText("echo: " + requestText.getText());
048                                /**
049                                 * done
050                                 */
051                                Thread.sleep(5000);
052                                replyProducer.send(reply);
053                                replyProducer.close();
054                        }
055                } catch (JMSException | InterruptedException e) {
056                        System.err.println(e);
057                }
058
059        }
060
061        public String revers(String in) {
062                String out = "";
063                for (char c : in.toCharArray()) {
064                        out = c + out;
065                }
066                return out;
067        }
068
069        public static void main(String[] args) {
070                Umdreher node = null;
071                try {
072                        node = new Umdreher();
073                        while (true) {
074                                Thread.sleep(1000);
075                        }
076                } catch (InterruptedException | NamingException | JMSException e) {
077                        System.err.println(e);
078                } finally {
079                        try {
080                                if ((node != null) && (node.consumer != null)) {
081                                        node.consumer.close();
082                                }
083                                if ((node != null) && (node.session != null)) {
084                                        node.session.close();
085                                }
086                                if ((node != null) && (node.connection != null)) {
087                                        node.connection.close();
088                                }
089                        } catch (JMSException e) {
090                                System.err.println(e);
091                        }
092                }
093        }
094}