List of usage examples for javax.jms TextMessage setText
void setText(String string) throws JMSException;
From source file:org.apache.flume.source.jms.TestIntegrationActiveMQ.java
private void putTopic(List<String> events) throws Exception { ConnectionFactory factory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKER_BIND_URL); Connection connection = factory.createConnection(); connection.start();/*from ww w . j a v a 2s. c om*/ Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createTopic(DESTINATION_NAME); MessageProducer producer = session.createProducer(destination); for (String event : events) { TextMessage message = session.createTextMessage(); message.setText(event); producer.send(message); } session.commit(); session.close(); connection.close(); }
From source file:Chat.java
/** Create JMS client for publishing and subscribing to messages. */ private void chatter(String broker, String username, String password) { // Create a connection. try {//from ww w .j ava 2 s . co m javax.jms.ConnectionFactory factory; factory = new ActiveMQConnectionFactory(username, password, broker); connect = factory.createConnection(username, password); pubSession = connect.createSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE); subSession = connect.createSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE); } catch (javax.jms.JMSException jmse) { System.err.println("error: Cannot connect to Broker - " + broker); jmse.printStackTrace(); System.exit(1); } // Create Publisher and Subscriber to 'chat' topics try { javax.jms.Topic topic = pubSession.createTopic(APP_TOPIC); javax.jms.MessageConsumer subscriber = subSession.createConsumer(topic); subscriber.setMessageListener(this); publisher = pubSession.createProducer(topic); // Now that setup is complete, start the Connection connect.start(); } catch (javax.jms.JMSException jmse) { jmse.printStackTrace(); } try { // Read all standard input and send it as a message. java.io.BufferedReader stdin = new java.io.BufferedReader(new java.io.InputStreamReader(System.in)); System.out.println("\nChat application:\n" + "=================\n" + "The application user " + username + " connects to the broker at " + DEFAULT_BROKER_NAME + ".\n" + "The application will publish messages to the " + APP_TOPIC + " topic.\n" + "The application also subscribes to that topic to consume any messages published there.\n\n" + "Type some text, and then press Enter to publish it as a TextMesssage from " + username + ".\n"); while (true) { String s = stdin.readLine(); if (s == null) exit(); else if (s.length() > 0) { javax.jms.TextMessage msg = pubSession.createTextMessage(); msg.setText(username + ": " + s); publisher.send(msg); } } } catch (java.io.IOException ioe) { ioe.printStackTrace(); } catch (javax.jms.JMSException jmse) { jmse.printStackTrace(); } }
From source file:org.apache.qpid.multiconsumer.AMQTest.java
public void testMultipleListeners() throws Exception { setup();//from w w w . j a v a 2s . co m try { // Create 5 listeners MsgHandler[] listeners = new MsgHandler[5]; for (int i = 0; i < listeners.length; i++) { listeners[i] = new MsgHandler(); MessageConsumer subscriber = subSession.createConsumer(topic); subscriber.setMessageListener(listeners[i]); } MessageProducer publisher = pubSession.createProducer(topic); // Send a single message TextMessage msg = pubSession.createTextMessage(); msg.setText(DUMMYCONTENT); publisher.send(msg); Thread.sleep(5000); // Check listeners to ensure they all got it for (int i = 0; i < listeners.length; i++) { if (listeners[i].isGotIt()) { System.out.println("Got callback for listener " + i); } else { TestCase.fail("Listener " + i + " did not get callback"); } } } catch (Throwable e) { System.err.println("Error: " + e); e.printStackTrace(System.err); } finally { close(); } }
From source file:Requestor.java
/** Create JMS client for sending messages. */ private void start(String broker, String username, String password, String sQueue) { // Create a connection. try {// w w w . j a v a 2 s . c o m javax.jms.QueueConnectionFactory factory; factory = new ActiveMQConnectionFactory(username, password, broker); connect = factory.createQueueConnection(username, password); session = connect.createQueueSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE); } catch (javax.jms.JMSException jmse) { System.err.println("error: Cannot connect to Broker - " + broker); jmse.printStackTrace(); System.exit(1); } // Create the Queue and QueueRequestor for sending requests. javax.jms.Queue queue = null; try { queue = session.createQueue(sQueue); requestor = new javax.jms.QueueRequestor(session, queue); // Now that all setup is complete, start the Connection. connect.start(); } catch (javax.jms.JMSException jmse) { jmse.printStackTrace(); exit(); } try { // Read all standard input and send it as a message. java.io.BufferedReader stdin = new java.io.BufferedReader(new java.io.InputStreamReader(System.in)); System.out.println("\nRequestor application:\n" + "============================\n" + "The application user " + username + " connects to the broker at " + DEFAULT_BROKER_NAME + ".\n" + "The application uses a QueueRequestor to on the " + DEFAULT_QUEUE + " queue." + "The Replier application gets the message, and transforms it." + "The Requestor application displays the result.\n\n" + "Type some mixed case text, and then press Enter to make a request.\n"); while (true) { String s = stdin.readLine(); if (s == null) exit(); else if (s.length() > 0) { javax.jms.TextMessage msg = session.createTextMessage(); msg.setText(username + ": " + s); // Instead of sending, we will use the QueueRequestor. javax.jms.Message response = requestor.request(msg); // The message should be a TextMessage. Just report it. javax.jms.TextMessage textMessage = (javax.jms.TextMessage) response; System.out.println("[Reply] " + textMessage.getText()); } } } catch (java.io.IOException ioe) { ioe.printStackTrace(); } catch (javax.jms.JMSException jmse) { jmse.printStackTrace(); } }
From source file:net.blogracy.controller.FileSharing.java
public void downloadByHash(final String hash) { try {/* ww w. j av a 2 s . co m*/ JSONObject sharedFile = new JSONObject(); sharedFile.put("uri", "magnet:?xt=urn:btih:" + hash); sharedFile.put("file", CACHE_FOLDER + File.separator + hash); TextMessage message = session.createTextMessage(); message.setText(sharedFile.toString()); producer.send(downloadQueue, message); } catch (Exception e) { e.printStackTrace(); } }
From source file:it.cnr.isti.labse.glimpse.manager.GlimpseManager.java
private TextMessage createMessage(String msg, String sender) { try {/*from ww w. j a v a 2 s. c om*/ TextMessage sendMessage = publishSession.createTextMessage(); sendMessage.setText(msg); sendMessage.setStringProperty("DESTINATION", sender); return sendMessage; } catch (JMSException e) { e.printStackTrace(); return null; } }
From source file:net.blogracy.controller.FileSharing.java
public String seed(File file) { String uri = null;//w w w .j av a 2s.c o m try { Destination tempDest = session.createTemporaryQueue(); MessageConsumer responseConsumer = session.createConsumer(tempDest); JSONObject requestObj = new JSONObject(); requestObj.put("file", file.getAbsolutePath()); TextMessage request = session.createTextMessage(); request.setText(requestObj.toString()); request.setJMSReplyTo(tempDest); producer.send(seedQueue, request); TextMessage response = (TextMessage) responseConsumer.receive(); String msgText = ((TextMessage) response).getText(); JSONObject responseObj = new JSONObject(msgText); uri = responseObj.getString("uri"); } catch (Exception e) { e.printStackTrace(); } return uri; }
From source file:com.nesscomputing.jms.JsonProducerCallback.java
@Override @CheckForNull/* ww w. ja va 2 s . c om*/ public Message buildMessage(final AbstractProducer<Object> producer, final Object data) throws IOException, JMSException { Preconditions.checkState(mapper != null, "need object mapper configured!"); final TextMessage message = producer.createTextMessage(); if (message == null) { throw new JMSException("Could not create text message, not connected?"); } else { final String dataText = mapper.writeValueAsString(data); message.setText(dataText); return message; } }
From source file:org.mule.transport.jms.integration.JmsTransformersTestCase.java
@Test public void testTransformTextMessage() throws Exception { RequestContext.setEvent(getTestEvent("test")); String text = "This is a test TextMessage"; TextMessage tMsg = session.createTextMessage(); tMsg.setText(text); AbstractJmsTransformer trans = createObject(JMSMessageToObject.class); Object result = trans.transform(tMsg); assertTrue("Transformed object should be a string", text.equals(result.toString())); AbstractJmsTransformer trans2 = new SessionEnabledObjectToJMSMessage(session); trans2.setReturnDataType(DataTypeFactory.create(TextMessage.class)); initialiseObject(trans2);//from w w w . j a v a 2s . c o m Object result2 = trans2.transform(text); assertTrue("Transformed object should be a TextMessage", result2 instanceof TextMessage); }
From source file:eu.learnpad.simulator.mon.manager.GlimpseManager.java
private TextMessage createMessage(String msg, String sender) { try {//from w ww .j av a 2s .co m TextMessage sendMessage = publishSession.createTextMessage(); sendMessage.setText(msg); sendMessage.setStringProperty("DESTINATION", sender); sendMessage.setBooleanProperty("ISASCORE", false); return sendMessage; } catch (JMSException e) { e.printStackTrace(); return null; } }