001package var.mom.jms.tasks; 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.JMSException; 010import javax.jms.Message; 011import javax.jms.MessageConsumer; 012import javax.jms.MessageListener; 013import javax.jms.MessageProducer; 014import javax.jms.Queue; 015import javax.jms.Session; 016import javax.jms.TextMessage; 017import javax.naming.Context; 018import javax.naming.InitialContext; 019import javax.naming.NamingException; 020 021import var.mom.jms.echo.Conf; 022 023public class Anfrager implements MessageListener { 024 private Connection connection; 025 private Session session; 026 private MessageProducer producer; 027 private MessageConsumer consumer; 028 private Queue replyQueue; 029 030 public Anfrager() throws NamingException, JMSException { 031 Context ctx = new InitialContext(); 032 ConnectionFactory factory = (ConnectionFactory) ctx.lookup("ConnectionFactory"); 033 this.connection = factory.createConnection(); 034 this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 035 Queue queue = (Queue) ctx.lookup(Conf.QUEUE); 036 this.producer = this.session.createProducer(queue); 037 this.replyQueue = this.session.createTemporaryQueue(); 038 this.consumer = this.session.createConsumer(this.replyQueue); 039 040 // Listener bekannt machen 041 this.consumer.setMessageListener(this); 042 043 this.connection.start(); 044 } 045 /* 046 * ## kann wohl entfallen ? 047 * 048 * public void receiveAndPrintMessages() throws JMSException { Message request; 049 * while ((request = consumer.receive()) != null) { try { if (request instanceof 050 * TextMessage) { TextMessage requestText = (TextMessage) request; String 051 * messageText = requestText.getText(); System.out.println("empfangen: " + 052 * messageText); } } catch (JMSException e) { System.err.println(e); } } } 053 */ 054 // ## �bernahme aus ConsumerCallBackNode 055 056 @Override 057 public void onMessage(Message message) { 058 try { 059 if (message instanceof TextMessage) { 060 TextMessage textMessage = (TextMessage) message; 061 String messageText = textMessage.getText(); 062 String priority = textMessage.getStringProperty("Priority"); 063 System.out.println(messageText + " [Priority=" + priority + "]"); 064 } 065 } catch (JMSException e) { 066 System.err.println(e); 067 } 068 069 } 070 071 public void sendMessage(String text) throws JMSException { 072 TextMessage message = this.session.createTextMessage(); 073 message.setText(text); 074 message.setJMSReplyTo(this.replyQueue); 075 this.producer.send(message); 076 } 077 078 public static void main(String[] args) throws IOException { 079 // String text = args[0]; 080 Anfrager node = null; 081 try { 082 083 BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); 084 String line; 085 /** 086 * insert 087 */ 088 while (true) { 089 line = input.readLine(); 090 node.sendMessage(line); 091 092 node = new Anfrager(); 093 node.sendMessage("xyz"); 094 /* 095 * kann ja dann nicht gehen node.receiveAndPrintMessages(); 096 */ 097 } 098 /** 099 * done 100 */ 101 } catch (NamingException | JMSException e) { 102 System.err.println(e); 103 } finally { 104 try { 105 if ((node != null) && (node.producer != null)) { 106 node.producer.close(); 107 } 108 if ((node != null) && (node.consumer != null)) { 109 node.consumer.close(); 110 } 111 if ((node != null) && (node.session != null)) { 112 node.session.close(); 113 } 114 if ((node != null) && (node.connection != null)) { 115 node.connection.close(); 116 } 117 } catch (JMSException e) { 118 System.err.println(e); 119 } 120 } 121 } 122}