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