|
| 1 | +package com.wso2.support.tipco; |
| 2 | + |
| 3 | +import com.tibco.tibjms.Tibjms; |
| 4 | +import com.tibco.tibjms.TibjmsTextMessage; |
| 5 | + |
| 6 | +import javax.jms.Connection; |
| 7 | +import javax.jms.ConnectionFactory; |
| 8 | +import javax.jms.Destination; |
| 9 | +import javax.jms.ExceptionListener; |
| 10 | +import javax.jms.JMSException; |
| 11 | +import javax.jms.Message; |
| 12 | +import javax.jms.MessageConsumer; |
| 13 | +import javax.jms.MessageProducer; |
| 14 | +import javax.jms.Session; |
| 15 | +import javax.jms.TextMessage; |
| 16 | +import java.util.Vector; |
| 17 | + |
| 18 | +//Libraries needed to build SOAP message |
| 19 | + |
| 20 | +public class ConsumerProducer implements ExceptionListener { |
| 21 | + |
| 22 | + private boolean useTopic = false; |
| 23 | + private int ackMode = Session.AUTO_ACKNOWLEDGE; |
| 24 | + private String corelation_id = null; |
| 25 | + |
| 26 | + private Session consumer_session = null; |
| 27 | + private Session producer_session = null; |
| 28 | + |
| 29 | + private MessageProducer msgProducer = null; |
| 30 | + |
| 31 | + private Vector<String> data = new Vector<String>(); |
| 32 | + |
| 33 | + private ConsumerProducer() { |
| 34 | + try { |
| 35 | + |
| 36 | + String serverUrl = "tcp://localhost:7222"; |
| 37 | + ConnectionFactory consumer_factory = new com.tibco.tibjms.TibjmsConnectionFactory(serverUrl); |
| 38 | + |
| 39 | + /* create the connection */ |
| 40 | + String userName = "admin"; |
| 41 | + String password = ""; |
| 42 | + Connection consumer_connection = consumer_factory.createConnection(userName, password); |
| 43 | + |
| 44 | + /* create the session */ |
| 45 | + consumer_session = consumer_connection.createSession(ackMode); |
| 46 | + |
| 47 | + /* set the exception listener */ |
| 48 | + consumer_connection.setExceptionListener(this); |
| 49 | + |
| 50 | + /* start the connection */ |
| 51 | + consumer_connection.start(); |
| 52 | + |
| 53 | + ConnectionFactory producer_factory = new com.tibco.tibjms.TibjmsConnectionFactory(serverUrl); |
| 54 | + |
| 55 | + Connection producer_connection = producer_factory.createConnection(userName, password); |
| 56 | + |
| 57 | + /* create the session */ |
| 58 | + producer_session = producer_connection.createSession(javax.jms.Session.AUTO_ACKNOWLEDGE); |
| 59 | + |
| 60 | + producer_connection.start(); |
| 61 | + |
| 62 | + Consume(); |
| 63 | + } catch (JMSException e) { |
| 64 | + e.printStackTrace(); |
| 65 | + } |
| 66 | + |
| 67 | + } |
| 68 | + |
| 69 | + public static void main(String[] args) { |
| 70 | + |
| 71 | + new ConsumerProducer(); |
| 72 | + } |
| 73 | + |
| 74 | + private void Consume() throws JMSException { |
| 75 | + Message msg = null; |
| 76 | + String msgType = "UNKNOWN"; |
| 77 | + |
| 78 | + String consumer_queue_name = "Request"; |
| 79 | + System.err.println("Subscribing to destination: " + consumer_queue_name + "\n"); |
| 80 | + |
| 81 | + |
| 82 | + /* create the destination */ |
| 83 | + Destination consumer_destination = null; |
| 84 | + if (useTopic) |
| 85 | + consumer_destination = consumer_session.createTopic(consumer_queue_name); |
| 86 | + else |
| 87 | + consumer_destination = consumer_session.createQueue(consumer_queue_name); |
| 88 | + |
| 89 | + /* create the consumer */ |
| 90 | + MessageConsumer msgConsumer = consumer_session.createConsumer(consumer_destination); |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | + /* read messages */ |
| 95 | + while (true) { |
| 96 | + /* receive the message */ |
| 97 | + msg = msgConsumer.receive(); |
| 98 | + //corelation_id = msg.getJMSCorrelationID(); |
| 99 | + corelation_id = msg.getJMSMessageID(); |
| 100 | + //corelation_id=msg.getJMSCorrelationID(); |
| 101 | + System.err.println(System.currentTimeMillis() + "++++++++++++++++++++" + corelation_id |
| 102 | + + "++++++++++++++++++++++++++++++++" + "message recieved"); |
| 103 | + if (ackMode == Session.CLIENT_ACKNOWLEDGE || ackMode == Tibjms.EXPLICIT_CLIENT_ACKNOWLEDGE |
| 104 | + || ackMode == Tibjms.EXPLICIT_CLIENT_DUPS_OK_ACKNOWLEDGE) { |
| 105 | + msg.acknowledge(); |
| 106 | + } |
| 107 | + /* try { |
| 108 | + TimeUnit.SECONDS.sleep(60); |
| 109 | + }catch (Exception e){ |
| 110 | + System.err.println(e.toString()); |
| 111 | + }*/ |
| 112 | + |
| 113 | + data.clear(); |
| 114 | + data.add(msg.toString()); |
| 115 | + System.err.println("is get delivery ++++++++++++++++++" + msg.getJMSRedelivered()); |
| 116 | + if (((TibjmsTextMessage) msg).getText().equals("<outonly/>") && msg.getJMSReplyTo() != null) { |
| 117 | + System.out.println("!!!Suspicious message, found reply to (getJMSReplyTo) in out only message !!! "); |
| 118 | + continue; |
| 119 | + } |
| 120 | + publish(msg.getJMSReplyTo()); |
| 121 | + |
| 122 | + } |
| 123 | + |
| 124 | + } |
| 125 | + |
| 126 | + private void publish(Destination replyDest) { |
| 127 | + if (replyDest != null) { |
| 128 | + |
| 129 | + System.err.println("reply destination++++++++++++++++++++++++++++" + replyDest.toString()); |
| 130 | + |
| 131 | + try { |
| 132 | + TextMessage msg; |
| 133 | + int i; |
| 134 | + |
| 135 | + if (data.size() == 0) { |
| 136 | + System.err.println("***Error: must specify at least one message text\n"); |
| 137 | + } |
| 138 | + |
| 139 | + System.err.println("Publishing to destination '" + replyDest); |
| 140 | + |
| 141 | + /* create the destination */ |
| 142 | + Destination producer_destination = null; |
| 143 | + String producer_queue_name = "Response"; |
| 144 | + if (useTopic) |
| 145 | + producer_destination = producer_session.createTopic(producer_queue_name); |
| 146 | + else { |
| 147 | + // producer_destination = producer_session.createQueue(replyDest.toString()); |
| 148 | + /* create the producer */ |
| 149 | + msgProducer = producer_session.createProducer(replyDest); |
| 150 | + |
| 151 | + } |
| 152 | + |
| 153 | + /* publish messages */ |
| 154 | + for (i = 0; i < data.size(); i++) { |
| 155 | + /* create text message */ |
| 156 | + msg = producer_session.createTextMessage(); |
| 157 | + msg.setJMSCorrelationID(corelation_id); |
| 158 | + |
| 159 | + /* set message text */ |
| 160 | + // msg.setText(data.elementAt(i)); |
| 161 | + // msg.setText("{\"hello\": \"world\"}"); |
| 162 | + // msg.setStringProperty("Content-Type", "application/json"); |
| 163 | + |
| 164 | + msg.setText("<div>Hello World</div>"); |
| 165 | + msg.setStringProperty("Content-Type", "application/xml"); |
| 166 | + |
| 167 | + /* publish message */ |
| 168 | + |
| 169 | + try { |
| 170 | + Thread.sleep(300); |
| 171 | + } catch (Exception e) { |
| 172 | + System.err.println(e); |
| 173 | + } |
| 174 | + msgProducer.send(msg); |
| 175 | + |
| 176 | + System.err.println(System.currentTimeMillis() + "+++++++++++++++++++++++++++++++" + corelation_id |
| 177 | + + "++++++++++++++++++++++++++++++++++++ message published \n"); |
| 178 | + } |
| 179 | + |
| 180 | + msgProducer.close(); |
| 181 | + |
| 182 | + /* close the connection */ |
| 183 | + // producer_connection.close(); |
| 184 | + } catch (JMSException e) { |
| 185 | + e.printStackTrace(); |
| 186 | + System.exit(-1); |
| 187 | + } |
| 188 | + } else { |
| 189 | + System.out.println("Reply to Dest null"); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + public void onException(JMSException e) { |
| 194 | + /* print the connection exception status */ |
| 195 | + System.err.println("CONNECTION EXCEPTION: " + e.getMessage()); |
| 196 | + } |
| 197 | + |
| 198 | +} |
0 commit comments