List of usage examples for javax.jms MessageProducer send
void send(Message message) throws JMSException;
From source file:com.cognifide.aet.runner.distribution.RunnerMessageListener.java
private void sendFatalMessage(Message wrapperMessage, String message) { try {/*from w w w. j a v a 2 s .com*/ final Destination jmsReplyTo = wrapperMessage.getJMSReplyTo(); if (jmsReplyTo != null) { final MessageProducer producer = session.createProducer(jmsReplyTo); final FatalErrorMessage errorMessage = new FatalErrorMessage( "Failed to process test suite: " + message, wrapperMessage.getJMSCorrelationID()); producer.send(session.createObjectMessage(errorMessage)); producer.close(); } } catch (Exception e) { LOGGER.error("Fatal exception, can't deliver message {}!", message, e); } }
From source file:Supplier.java
public void run() { ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url); Session session = null;/* ww w .ja va 2 s . c om*/ Destination orderQueue; try { Connection connection = connectionFactory.createConnection(); session = connection.createSession(true, Session.SESSION_TRANSACTED); orderQueue = session.createQueue(QUEUE); MessageConsumer consumer = session.createConsumer(orderQueue); connection.start(); while (true) { Message message = consumer.receive(); MessageProducer producer = session.createProducer(message.getJMSReplyTo()); MapMessage orderMessage; if (message instanceof MapMessage) { orderMessage = (MapMessage) message; } else { // End of Stream producer.send(session.createMessage()); session.commit(); producer.close(); break; } int quantity = orderMessage.getInt("Quantity"); System.out.println( ITEM + " Supplier: Vendor ordered " + quantity + " " + orderMessage.getString("Item")); MapMessage outMessage = session.createMapMessage(); outMessage.setInt("VendorOrderNumber", orderMessage.getInt("VendorOrderNumber")); outMessage.setString("Item", ITEM); quantity = Math.min(orderMessage.getInt("Quantity"), new Random().nextInt(orderMessage.getInt("Quantity") * 10)); outMessage.setInt("Quantity", quantity); producer.send(outMessage); System.out.println(ITEM + " Supplier: Sent " + quantity + " " + ITEM + "(s)"); session.commit(); System.out.println(ITEM + " Supplier: committed transaction"); producer.close(); } connection.close(); } catch (JMSException e) { e.printStackTrace(); } }
From source file:eu.semaine.jms.JMSLogger.java
private void log(MessageProducer target, Level level, long usertime, Object... objects) { String logMessageText = toLogMessageText(usertime, objects); try {//from w w w . j a va 2s . c om TextMessage message = session.createTextMessage(logMessageText); target.send(message); } catch (JMSException e) { fallbackLogger.error("problem with JMS logger", e); if (level == Level.error) { fallbackLogger.error(logMessageText); } else if (level == Level.warn) { fallbackLogger.warn(logMessageText); } else if (level == Level.info) { fallbackLogger.warn(logMessageText); } else if (level == Level.debug) { fallbackLogger.warn(logMessageText); } else { fallbackLogger.error("problem with fallback logger -- unknown log level: " + level); fallbackLogger.info(logMessageText); } } }
From source file:org.apache.activemq.camel.JmsJdbcXARollbackTest.java
private void sendJMSMessageToKickOffRoute() throws Exception { ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("vm://testXA"); factory.setWatchTopicAdvisories(false); Connection connection = factory.createConnection(); connection.start();/*from ww w . java2 s.com*/ Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer producer = session.createProducer(new ActiveMQQueue("scp_transacted")); TextMessage message = session.createTextMessage("Some Text, messageCount:" + messageCount++); message.setJMSCorrelationID("pleaseCorrelate"); producer.send(message); connection.close(); }
From source file:org.apache.flume.source.jms.TestIntegrationActiveMQ.java
private void putQueue(List<String> events) throws Exception { ConnectionFactory factory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKER_BIND_URL); Connection connection = factory.createConnection(); connection.start();//from w ww . java 2 s . c om Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createQueue(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: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 . ja v a 2 s. c o m 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:com.oneops.inductor.MessagePublisher.java
@Override protected boolean process(MessageHolder holder) { Map<String, String> event = holder.getMap(); try {//from w w w.java 2s. c o m TextMessage message = session.createTextMessage(event.get("body")); message.setJMSCorrelationID(event.get("correlationID")); for (Map.Entry<String, String> kv : event.entrySet()) { if (!kv.getKey().equals("body")) { message.setStringProperty(kv.getKey(), kv.getValue()); } } MessageProducer producer = regularProducer; if ("high".equals(event.get("priority"))) { producer = priorityProducer; logger.debug("using priority producer to publish message"); } producer.send(message); logger.debug("Published: " + message.toString()); return true; } catch (NullPointerException npe) { // happens when amq session is null logger.warn("caught NullPointerException - reconnecting to broker"); waitSome(); init(); return false; } catch (IllegalStateException e) { // this happens when connection is hosed - lets re-init logger.warn("caught IllegalStateException - reconnecting to broker"); init(); return false; } catch (JMSException e) { logger.error(e.getMessage()); logger.debug(e.getMessage(), e); return false; } }
From source file:com.microsoft.azure.servicebus.samples.jmstopicquickstart.JmsTopicQuickstart.java
public void run(String connectionString) throws Exception { // The connection string builder is the only part of the azure-servicebus SDK library // we use in this JMS sample and for the purpose of robustly parsing the Service Bus // connection string. ConnectionStringBuilder csb = new ConnectionStringBuilder(connectionString); // set up the JNDI context Hashtable<String, String> hashtable = new Hashtable<>(); hashtable.put("connectionfactory.SBCF", "amqps://" + csb.getEndpoint().getHost() + "?amqp.idleTimeout=120000&amqp.traceFrames=true"); hashtable.put("topic.TOPIC", "BasicTopic"); hashtable.put("queue.SUBSCRIPTION1", "BasicTopic/Subscriptions/Subscription1"); hashtable.put("queue.SUBSCRIPTION2", "BasicTopic/Subscriptions/Subscription2"); hashtable.put("queue.SUBSCRIPTION3", "BasicTopic/Subscriptions/Subscription3"); hashtable.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.qpid.jms.jndi.JmsInitialContextFactory"); Context context = new InitialContext(hashtable); ConnectionFactory cf = (ConnectionFactory) context.lookup("SBCF"); // Look up the topic Destination topic = (Destination) context.lookup("TOPIC"); // we create a scope here so we can use the same set of local variables cleanly // again to show the receive side seperately with minimal clutter {/*from w w w . ja v a 2 s . com*/ // Create Connection Connection connection = cf.createConnection(csb.getSasKeyName(), csb.getSasKey()); connection.start(); // Create Session, no transaction, client ack Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE); // Create producer MessageProducer producer = session.createProducer(topic); // Send messaGES for (int i = 0; i < totalSend; i++) { BytesMessage message = session.createBytesMessage(); message.writeBytes(String.valueOf(i).getBytes()); producer.send(message); System.out.printf("Sent message %d.\n", i + 1); } producer.close(); session.close(); connection.stop(); connection.close(); } // Look up the subscription (pretending it's a queue) receiveFromSubscription(csb, context, cf, "SUBSCRIPTION1"); receiveFromSubscription(csb, context, cf, "SUBSCRIPTION2"); receiveFromSubscription(csb, context, cf, "SUBSCRIPTION3"); System.out.printf("Received all messages, exiting the sample.\n"); System.out.printf("Closing queue client.\n"); }
From source file:com.microsoft.azure.servicebus.samples.jmsqueuequickstart.JmsQueueQuickstart.java
public void run(String connectionString) throws Exception { // The connection string builder is the only part of the azure-servicebus SDK library // we use in this JMS sample and for the purpose of robustly parsing the Service Bus // connection string. ConnectionStringBuilder csb = new ConnectionStringBuilder(connectionString); // set up JNDI context Hashtable<String, String> hashtable = new Hashtable<>(); hashtable.put("connectionfactory.SBCF", "amqps://" + csb.getEndpoint().getHost() + "?amqp.idleTimeout=120000&amqp.traceFrames=true"); hashtable.put("queue.QUEUE", "BasicQueue"); hashtable.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.qpid.jms.jndi.JmsInitialContextFactory"); Context context = new InitialContext(hashtable); ConnectionFactory cf = (ConnectionFactory) context.lookup("SBCF"); // Look up queue Destination queue = (Destination) context.lookup("QUEUE"); // we create a scope here so we can use the same set of local variables cleanly // again to show the receive side separately with minimal clutter {/* w ww. java 2 s .com*/ // Create Connection Connection connection = cf.createConnection(csb.getSasKeyName(), csb.getSasKey()); // Create Session, no transaction, client ack Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE); // Create producer MessageProducer producer = session.createProducer(queue); // Send messages for (int i = 0; i < totalSend; i++) { BytesMessage message = session.createBytesMessage(); message.writeBytes(String.valueOf(i).getBytes()); producer.send(message); System.out.printf("Sent message %d.\n", i + 1); } producer.close(); session.close(); connection.stop(); connection.close(); } { // Create Connection Connection connection = cf.createConnection(csb.getSasKeyName(), csb.getSasKey()); connection.start(); // Create Session, no transaction, client ack Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE); // Create consumer MessageConsumer consumer = session.createConsumer(queue); // create a listener callback to receive the messages consumer.setMessageListener(message -> { try { // receives message is passed to callback System.out.printf("Received message %d with sq#: %s\n", totalReceived.incrementAndGet(), // increments the tracking counter message.getJMSMessageID()); message.acknowledge(); } catch (Exception e) { logger.error(e); } }); // wait on the main thread until all sent messages have been received while (totalReceived.get() < totalSend) { Thread.sleep(1000); } consumer.close(); session.close(); connection.stop(); connection.close(); } System.out.printf("Received all messages, exiting the sample.\n"); System.out.printf("Closing queue client.\n"); }
From source file:org.apache.qpid.multiconsumer.AMQTest.java
public void testMultipleListeners() throws Exception { setup();//from ww w .ja va 2 s. com 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(); } }