...
This example is also available under the examples scripting directory that ships with SOAtest. It is included as an Eclipse project in a zip archive that can be imported to your Eclipse workspace. It requires tibjmsthe tibjms.jar from jar file from TIBCO EMS and the com.parasoft.api.jar to jar file to be added to the classpath in order to build and run. The com.parasoft.api.jar is available at <SOAtest Installation Directory>\plugins\jar is located in the <INSTALL>/plugins/com.parasoft.xtestptest.libs.web_<SOAtest version>\<VERSION>/root directory.
Code Block |
---|
import com.parasoft.api.*; import com.tibco.tibjms.*; import javax.jms.*; public class TIBCOEventSubscriber extends EventSubscriber { private ConsumerRunnable _consumerRunnable; private Connection _connection; private String _dest; private boolean _started = false; public TIBCOEventSubscriber(String url, String username, String password, String destination) throws JMSException { _dest = destination; QueueConnectionFactory factory = new TibjmsQueueConnectionFactory(url); _connection = factory.createQueueConnection(username, password); } public boolean start() throws Exception { Session session = null; session = _connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createTopic(_dest); MessageConsumer msgConsumer = session.createConsumer(destination); _connection.start(); _started = true; _consumerRunnable = new ConsumerRunnable(msgConsumer); Thread thread = new Thread(_consumerRunnable); thread.start(); Application.showMessage("Monitoring started on " + _dest); return true; } public boolean stop() throws Exception { _started = false; Thread.sleep(1000); if (_connection != null) { _connection.close(); Application.showMessage("Monitoring connection closed"); } if (_consumerRunnable != null && _consumerRunnable.getException() != null) { throw _consumerRunnable.getException(); } return _started; } private class ConsumerRunnable implements Runnable { private MessageConsumer msgConsumer; private Exception t; public ConsumerRunnable(MessageConsumer msgConsumer) { this.msgConsumer = msgConsumer; } public void run() { while(_started) { try { MapMessage msg = (MapMessage)msgConsumer.receive(500); if (msg == null) { continue; } Message actualMessage = null; try { byte[] bytes = msg.getBytes("message_bytes"); if (bytes != null && bytes.length > 0) { actualMessage = Tibjms.createFromBytes(bytes); } } catch (JMSException e1) { } // you can provide your own extension of Event in order to customize the event // getLabel(), toString() and toXML() outputs IEvent event; if (actualMessage == null) { event = new Event(msg); } else { event = new Event(actualMessage); } event.setTimestamp(msg.getJMSTimestamp()); onEvent(event); } catch (JMSException e) { Application.showMessage(e.getClass().toString() + ": " + e.getMessage()); try { msgConsumer.close(); } catch (JMSException e1) { Application.showMessage(e1.getClass().toString() + ": " + e1.getMessage()); } t = e; _started = false; } } } public Exception getException() { return t; } } } |
...