001package var.mom.jms.echo; 002 003import javax.jms.Connection; 004import javax.jms.ConnectionFactory; 005import javax.jms.JMSException; 006import javax.jms.Message; 007import javax.jms.MessageConsumer; 008import javax.jms.MessageProducer; 009import javax.jms.Queue; 010import javax.jms.Session; 011import javax.jms.TextMessage; 012import javax.naming.Context; 013import javax.naming.InitialContext; 014import javax.naming.NamingException; 015 016/** 017 * requester node for the echo service 018 * 019 * @author Sandro Leuchter 020 * 021 */ 022public class EchoRequesterNode { 023 private Connection connection; 024 private Session session; 025 private MessageProducer producer; 026 private MessageConsumer consumer; 027 private Queue replyQueue; 028 029 /** 030 * constructor, establishes and starts connection to JMS provider specified in 031 * JNDI (via jndi.properties), afterwards producer and consumer are ready 032 * 033 * @throws NamingException JNDI exceptions 034 * @throws JMSException JMS exceptions 035 */ 036 public EchoRequesterNode() throws NamingException, JMSException { 037 Context ctx = new InitialContext(); 038 ConnectionFactory factory = (ConnectionFactory) ctx.lookup("ConnectionFactory"); 039 this.connection = factory.createConnection(); 040 this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 041 Queue queue = (Queue) ctx.lookup(Conf.QUEUE); 042 this.producer = this.session.createProducer(queue); 043 this.replyQueue = this.session.createTemporaryQueue(); 044 this.consumer = this.session.createConsumer(this.replyQueue); 045 this.connection.start(); 046 } 047 048 /** 049 * synchronously receives the TextMessages in an infinite loop and prints 050 * payload text to StdOut 051 * 052 * @see javax.jms.TextMessage 053 * @throws JMSException JMS exceptions 054 */ 055 public void receiveAndPrintMessages() throws JMSException { 056 Message request; 057 while ((request = this.consumer.receive()) != null) { 058 try { 059 if (request instanceof TextMessage) { 060 TextMessage requestText = (TextMessage) request; 061 String messageText = requestText.getText(); 062 System.out.println("empfangen: " + messageText); 063 } 064 } catch (JMSException e) { 065 System.err.println(e); 066 } 067 } 068 } 069 070 /** 071 * creates TextMessage for JMS provider with temporary reply queue set and sends 072 * it to Destination of producer, which is configured in Conf.QUEUE 073 * 074 * @param text text to be send 075 * @throws JMSException JMS exceptions 076 */ 077 public void sendMessage(String text) throws JMSException { 078 TextMessage message = this.session.createTextMessage(); 079 message.setText(text); 080 message.setJMSReplyTo(this.replyQueue); 081 this.producer.send(message); 082 } 083 084 /** 085 * main routine and starting point of program 086 * 087 * @param args[0] text to be send to echo service replier 088 */ 089 public static void main(String[] args) { 090 String text = args[0]; 091 EchoRequesterNode node = null; 092 try { 093 node = new EchoRequesterNode(); 094 node.sendMessage(text); 095 node.receiveAndPrintMessages(); 096 } catch (NamingException | JMSException e) { 097 System.err.println(e); 098 } finally { 099 try { 100 if ((node != null) && (node.producer != null)) { 101 node.producer.close(); 102 } 103 if ((node != null) && (node.consumer != null)) { 104 node.consumer.close(); 105 } 106 if ((node != null) && (node.session != null)) { 107 node.session.close(); 108 } 109 if ((node != null) && (node.connection != null)) { 110 node.connection.close(); 111 } 112 } catch (JMSException e) { 113 System.err.println(e); 114 } 115 } 116 } 117}