List of usage examples for javax.jms Session createTopic
Topic createTopic(String topicName) throws JMSException;
From source file:org.apache.falcon.oozie.workflow.FalconPostProcessingTest.java
private void consumer(String brokerUrl, String topic, boolean checkUserMessage) throws JMSException { ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerUrl); Connection connection = connectionFactory.createConnection(); connection.start();// ww w . ja va2 s. c o m Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createTopic(topic); MessageConsumer consumer = session.createConsumer(destination); latch.countDown(); // Verify user message if (checkUserMessage) { verifyMesssage(consumer); } // Verify falcon message verifyMesssage(consumer); connection.close(); }
From source file:org.wso2.andes.systest.TestingBaseCase.java
/** * Perform the Main test of a topic Consumer with the given AckMode. * * Test creates a new connection and sets up the connection to prevent * failover//from w w w .ja va 2 s.com * * A new consumer is connected and started so that it will prefetch msgs. * * An asynchrounous publisher is started to fill the broker with messages. * * We then wait to be notified of the disconnection via the ExceptionListener * * 0-10 does not have the same notification paths but sync() apparently should * give us the exception, currently it doesn't, so the test is excluded from 0-10 * * We should ensure that this test has the same path for all protocol versions. * * Clients should not have to modify their code based on the protocol in use. * * @param ackMode @see javax.jms.Session * * @throws Exception */ protected void topicConsumer(int ackMode, boolean durable) throws Exception { Connection connection = getConnection(); connection.setExceptionListener(this); Session session = connection.createSession(ackMode == Session.SESSION_TRANSACTED, ackMode); _destination = session.createTopic(getName()); MessageConsumer consumer; if (durable) { consumer = session.createDurableSubscriber(_destination, getTestQueueName()); } else { consumer = session.createConsumer(_destination); } connection.start(); // Start the consumer pre-fetching // Don't care about response as we will fill the broker up with messages // after this point and ensure that the client is disconnected at the // right point. consumer.receiveNoWait(); startPublisher(_destination); boolean disconnected = _disconnectionLatch.await(DISCONNECTION_WAIT, TimeUnit.SECONDS); assertTrue("Client was not disconnected", disconnected); assertTrue("Client was not disconnected.", _connectionException != null); Exception linked = _connectionException.getLinkedException(); _publisher.join(JOIN_WAIT); assertFalse("Publisher still running", _publisher.isAlive()); //Validate publishing occurred ok if (_publisherError != null) { throw _publisherError; } // NOTE these exceptions will need to be modeled so that they are not // 0-8 specific. e.g. JMSSessionClosedException assertNotNull("No error received onException listener.", _connectionException); assertNotNull("No linked exception set on:" + _connectionException.getMessage(), linked); assertTrue("Incorrect linked exception received.", linked instanceof AMQException); AMQException amqException = (AMQException) linked; assertEquals("Channel was not closed with correct code.", AMQConstant.RESOURCE_ERROR, amqException.getErrorCode()); }
From source file:ProducerTool.java
public void run() { Connection connection = null; try {//from w w w.jav a 2 s. c om // Create the connection. ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url); connection = connectionFactory.createConnection(); connection.start(); // Create the session Session session = connection.createSession(transacted, Session.AUTO_ACKNOWLEDGE); if (topic) { destination = session.createTopic(subject); } else { destination = session.createQueue(subject); } // Create the producer. MessageProducer producer = session.createProducer(destination); if (persistent) { producer.setDeliveryMode(DeliveryMode.PERSISTENT); } else { producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); } if (timeToLive != 0) { producer.setTimeToLive(timeToLive); } // Start sending messages sendLoop(session, producer); System.out.println("[" + this.getName() + "] Done."); synchronized (lockResults) { ActiveMQConnection c = (ActiveMQConnection) connection; System.out.println("[" + this.getName() + "] Results:\n"); c.getConnectionStats().dump(new IndentPrinter()); } } catch (Exception e) { System.out.println("[" + this.getName() + "] Caught: " + e); e.printStackTrace(); } finally { try { connection.close(); } catch (Throwable ignore) { } } }
From source file:org.apache.activemq.network.MQTTNetworkOfBrokersFailoverTest.java
private CountDownLatch listenForConsumersOn(BrokerService broker) throws Exception { final CountDownLatch latch = new CountDownLatch(1); URI brokerUri = broker.getVmConnectorURI(); final ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory(brokerUri.toASCIIString()); final Connection connection = cf.createConnection(); connection.start();//from w w w. ja v a 2s. com final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Destination dest = session .createTopic("ActiveMQ.Advisory.Consumer.Queue.Consumer.foo:AT_LEAST_ONCE.VirtualTopic.foo.bar"); MessageConsumer consumer = session.createConsumer(dest); consumer.setMessageListener(new MessageListener() { @Override public void onMessage(Message message) { latch.countDown(); // shutdown this connection Dispatch.getGlobalQueue().execute(new Runnable() { @Override public void run() { try { session.close(); connection.close(); } catch (JMSException e) { e.printStackTrace(); } } }); } }); return latch; }
From source file:org.dawnsci.commandserver.core.producer.AliveConsumer.java
protected void startNotifications() throws Exception { if (uri == null) throw new NullPointerException("Please set the URI before starting notifications!"); this.cbean = new ConsumerBean(); cbean.setStatus(ConsumerStatus.STARTING); cbean.setName(getName());/*from ww w. j a va 2 s .co m*/ cbean.setConsumerId(consumerId); cbean.setVersion(consumerVersion); cbean.setStartTime(System.currentTimeMillis()); try { cbean.setHostName(InetAddress.getLocalHost().getHostName()); } catch (UnknownHostException e) { // Not fatal but would be nice... e.printStackTrace(); } System.out.println("Running events on topic " + Constants.ALIVE_TOPIC + " to notify of '" + getName() + "' service being available."); final Thread aliveThread = new Thread(new Runnable() { public void run() { try { ConnectionFactory connectionFactory = ConnectionFactoryFacade.createConnectionFactory(uri); aliveConnection = connectionFactory.createConnection(); aliveConnection.start(); final Session session = aliveConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); final Topic topic = session.createTopic(Constants.ALIVE_TOPIC); final MessageProducer producer = session.createProducer(topic); final ObjectMapper mapper = new ObjectMapper(); // Here we are sending the message out to the topic while (isActive()) { try { Thread.sleep(Constants.NOTIFICATION_FREQUENCY); TextMessage temp = session.createTextMessage(mapper.writeValueAsString(cbean)); producer.send(temp, DeliveryMode.NON_PERSISTENT, 1, 5000); if (ConsumerStatus.STARTING.equals(cbean.getStatus())) { cbean.setStatus(ConsumerStatus.RUNNING); } } catch (InterruptedException ne) { break; } catch (Exception neOther) { neOther.printStackTrace(); } } } catch (Exception ne) { ne.printStackTrace(); setActive(false); } } }); aliveThread.setName("Alive Notification Topic"); aliveThread.setDaemon(true); aliveThread.setPriority(Thread.MIN_PRIORITY); aliveThread.start(); }
From source file:de.klemp.middleware.controller.Controller.java
/** * This method creates a connection to the message broker Active MQ and * sends the message to the given topic. The method is used by all methods * of the second component.// w w w. j a v a2 s . c om * * @param message * send to the output device * @param topic * of the message broker */ public static void sendMessage(String message, String topic) { String url = ActiveMQConnection.DEFAULT_BROKER_URL; ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url); // Create a Connection Connection connection; try { connection = connectionFactory.createConnection(); connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // Create the destination (Topic or Queue) Destination destination = session.createTopic(topic); // Create a MessageProducer from the Session to the Topic or Queue MessageProducer producer = session.createProducer(destination); producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); // Create a messages TextMessage message1 = session.createTextMessage(message); // Tell the producer to send the message producer.send(message1); session.close(); connection.close(); } catch (JMSException e) { logger.error("Message could not be sended to activemq", e); } }
From source file:de.klemp.middleware.controller.Controller.java
public static void sendMessageFast(String message, String topic) { String url = ActiveMQConnection.DEFAULT_BROKER_URL; ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url); // Create a Connection Connection connection;/* www .ja v a 2 s .c o m*/ try { connectionFactory.setOptimizeAcknowledge(true); connectionFactory.setUseAsyncSend(true); connection = connectionFactory.createConnection(); connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // Create the destination (Topic or Queue) Destination destination = session.createTopic(topic); // Create a MessageProducer from the Session to the Topic or Queue MessageProducer producer = session.createProducer(destination); producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); // Create a messages TextMessage message1 = session.createTextMessage(message); // Tell the producer to send the message producer.send(message1); session.close(); connection.close(); } catch (JMSException e) { logger.error("Message could not be sended to activemq", e); } }
From source file:org.apache.activemq.JmsConnectionStartStopTest.java
/** * Tests if the consumer receives the messages that were sent before the * connection was started.// w w w .j a v a 2 s . c om * * @throws JMSException */ public void testStoppedConsumerHoldsMessagesTillStarted() throws JMSException { Session startedSession = startedConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); Session stoppedSession = stoppedConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); // Setup the consumers. Topic topic = startedSession.createTopic("test"); MessageConsumer startedConsumer = startedSession.createConsumer(topic); MessageConsumer stoppedConsumer = stoppedSession.createConsumer(topic); // Send the message. MessageProducer producer = startedSession.createProducer(topic); TextMessage message = startedSession.createTextMessage("Hello"); producer.send(message); // Test the assertions. Message m = startedConsumer.receive(1000); assertNotNull(m); m = stoppedConsumer.receive(1000); assertNull(m); stoppedConnection.start(); m = stoppedConsumer.receive(5000); assertNotNull(m); startedSession.close(); stoppedSession.close(); }
From source file:org.apache.falcon.messaging.JMSMessageConsumerTest.java
public void sendMessages(String topic, WorkflowExecutionContext.Type type, boolean isFalconWF) throws JMSException, FalconException, IOException { ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(BROKER_URL); Connection connection = connectionFactory.createConnection(); connection.start();/* www . j a v a2 s .c o m*/ Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Topic destination = session.createTopic(topic); javax.jms.MessageProducer producer = session.createProducer(destination); producer.setDeliveryMode(DeliveryMode.PERSISTENT); for (int i = 0; i < 5; i++) { Message message = null; switch (type) { case POST_PROCESSING: message = getMockFalconMessage(i, session); break; case WORKFLOW_JOB: message = getMockOozieMessage(i, session, isFalconWF); break; case COORDINATOR_ACTION: message = getMockOozieCoordMessage(i, session, isFalconWF); default: break; } Log.debug("Sending:" + message); producer.send(message); } }
From source file:org.wso2.carbon.bpmn.extensions.jms.JMSSender.java
/** * Sends a message to a queue/topic using shared connections, sessions and message producers * @param connectionFactory//from w ww .j a v a 2 s. c o m * @param text */ public void sendMessage(JMSConnectionFactory connectionFactory, int cacheLevel, String target, String text) { try { Connection connection = connectionFactory.getConnection(); Session session = connectionFactory.getSession(connection); Destination destination; if (connectionFactory.isQueue()) { destination = session.createQueue(target); if (destination == null) { } } else { destination = session.createTopic(target); } if (destination != null) { log.info("initializing jms message producer.."); MessageProducer producer = connectionFactory.getMessageProducer(connection, session, destination); JMSMessageSender jmsMessageSender = new JMSMessageSender(connection, session, producer, destination, cacheLevel, connectionFactory.isQueue()); TextMessage message = session.createTextMessage(text); jmsMessageSender.send(message, null); } else { log.error("Destination not specified"); } } catch (JMSException ex) { log.error(ex.getMessage(), ex); } }