Example usage for javax.jms Session createTemporaryTopic

List of usage examples for javax.jms Session createTemporaryTopic

Introduction

In this page you can find the example usage for javax.jms Session createTemporaryTopic.

Prototype


TemporaryTopic createTemporaryTopic() throws JMSException;

Source Link

Document

Creates a TemporaryTopic object.

Usage

From source file:org.ct.amq.jdbc.security.JdbcAuthenticationPluginTest.java

public void testTempDestinations() throws Exception {
    Connection conn = factory.createConnection("guest", "password");
    Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    String name = "org.apache.activemq:BrokerName=localhost,Type=TempTopic";
    try {//from  ww  w .jav a2  s  .  c o m
        conn.start();
        TemporaryTopic temp = sess.createTemporaryTopic();
        name += ",Destination=" + temp.getTopicName().replaceAll(":", "_");
        fail("Should have failed creating a temp topic");
    } catch (Exception ignore) {
    }

    ObjectName objName = new ObjectName(name);
    TopicViewMBean mbean = (TopicViewMBean) broker.getManagementContext().newProxyInstance(objName,
            TopicViewMBean.class, true);
    try {
        System.out.println(mbean.getName());
        fail("Shouldn't have created a temp topic");
    } catch (Exception ignore) {
    }
}

From source file:org.apache.activemq.usecases.RequestReplyToTopicViaThreeNetworkHopsTest.java

/**
 * TEST TEMPORARY TOPICS/*from  w w  w . j  a v  a  2s.c o m*/
 */
public void testTempTopic(String prod_broker_url, String cons_broker_url) throws Exception {
    Connection conn;
    Session sess;
    Destination cons_dest;
    int num_msg;

    num_msg = 5;

    LOG.debug("TESTING TEMP TOPICS " + prod_broker_url + " -> " + cons_broker_url + " (" + num_msg
            + " messages)");

    //
    // Connect to the bus.
    //
    conn = createConnection(cons_broker_url);
    conn.start();
    sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

    //
    // Create the destination on which messages are being tested.
    //
    LOG.trace("Creating destination");
    cons_dest = sess.createTemporaryTopic();

    testOneDest(conn, sess, cons_dest, num_msg);

    //
    // Cleanup
    //
    sess.close();
    conn.close();
}

From source file:org.apache.axis2.transport.jms.JMSUtils.java

/**
 * Create a temp queue or topic for synchronous receipt of responses, when a reply destination
 * is not specified/*w w w . j  a va  2s.  c  o  m*/
 * @param session the JMS Session to use
 * @return a temporary Queue or Topic, depending on the session
 * @throws JMSException
 */
public static Destination createTemporaryDestination(Session session) throws JMSException {

    if (session instanceof QueueSession) {
        return session.createTemporaryQueue();
    } else {
        return session.createTemporaryTopic();
    }
}

From source file:org.logicblaze.lingo.jmx.remote.jms.MBeanJmsServerConnectionClient.java

/**
 * Construct this thing/* w  w  w  .j av a2 s .  c o  m*/
 * 
 * @param connection
 * @throws JMSException
 */
public MBeanJmsServerConnectionClient(MBeanJmsServerConnection connection, Connection jmsConnection)
        throws JMSException {
    super(connection);
    this.serverConnection = connection;
    Session jmsSession = jmsConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    replyToDestination = jmsSession.createTemporaryTopic();
    MessageConsumer consumer = jmsSession.createConsumer(replyToDestination);
    consumer.setMessageListener(this);
}

From source file:org.mule.transport.jms.Jms11Support.java

public Destination createTemporaryDestination(Session session, boolean topic) throws JMSException {
    if (session == null) {
        throw new IllegalArgumentException("Session cannot be null when creating a destination");
    }//from  w  ww.  j  a  va2 s  .  c  o m

    if (topic) {
        return session.createTemporaryTopic();
    } else {
        return session.createTemporaryQueue();
    }
}

From source file:tools.ConsumerTool.java

@Override
public void run() {
    Connection connection = null;
    Session session = null;
    try {/*from  w  w  w .j  a  v a  2 s .  com*/
        connection = connectionFactory.createConnection();
        if (clientId != null) {
            connection.setClientID(clientId);
        }
        connection.start();
        session = connection.createSession(transacted, acknowledgeMode);
        Destination destination = null;
        if (jndiLookupDestinations) {
            destination = (Destination) context.lookup(destinationName);
        } else {
            if (useQueueDestinations) {
                if (useTemporaryDestinations) {
                    destination = session.createTemporaryQueue();
                } else {
                    destination = session.createQueue(destinationName);
                }
            } else {
                if (useTemporaryDestinations) {
                    destination = session.createTemporaryTopic();
                } else {
                    destination = session.createTopic(destinationName);
                }
            }
        }

        if (useQueueBrowser) {
            runQueueBrowser(session, (Queue) destination);
        } else {
            MessageConsumer consumer = null;
            if (useQueueDestinations) { //Queues
                if (selector != null) {
                    consumer = session.createConsumer(destination, selector);
                } else {
                    consumer = session.createConsumer(destination);
                }
            } else { //Queues
                if (durable) { //Durable Subscribers
                    if (selector != null) {
                        consumer = session.createDurableSubscriber((Topic) destination, subscriptionName,
                                selector, false);
                    } else {
                        consumer = session.createDurableSubscriber((Topic) destination, subscriptionName);
                    }
                } else { //Non-Durable Subscribers
                    if (selector != null) {
                        consumer = session.createConsumer(destination, selector);
                    } else {
                        consumer = session.createConsumer(destination);
                    }
                }
            }

            if (useAsyncListener) {
                final Session consumerSession = session;

                final AtomicInteger perConsumerReceivedMessages = new AtomicInteger(0);
                consumer.setMessageListener(new MessageListener() {

                    @Override
                    public void onMessage(Message message) {
                        perConsumerReceivedMessages.incrementAndGet();
                        handleMessage(consumerSession, message, perConsumerReceivedMessages.get());
                    }
                });
                while (perConsumerReceivedMessages.get() < numMessages) {
                    Thread.sleep(100);
                }
            } else {
                int perConsumerReceivedMessages = 0;
                while (perConsumerReceivedMessages < numMessages) {

                    Message message = null;
                    if (receiveTimeoutMS > -1) {
                        message = consumer.receive(receiveTimeoutMS);
                    } else {
                        message = consumer.receive();
                    }

                    if (message != null) {
                        perConsumerReceivedMessages++;
                        handleMessage(session, message, perConsumerReceivedMessages);
                    }
                }
            }
            consumer.close();
        }

    } catch (Exception ex) {
        LOGGER.error("ConsumerTool hit exception: " + ex.getMessage(), ex);
    } finally {
        if (session != null) {
            try {
                session.close();
            } catch (JMSException e) {
                LOGGER.error("JMSException closing session", e);
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (JMSException e) {
                LOGGER.error("JMSException closing connection", e);
            }
        }
    }
}

From source file:tools.ProducerTool.java

@Override
public void run() {
    Connection connection = null;
    Session session = null;
    try {/*from   www  .  ja v a  2 s  . c  o m*/
        connection = connectionFactory.createConnection();
        if (clientId != null) {
            connection.setClientID(clientId);
        }
        session = connection.createSession(transacted, acknowledgeMode);
        Destination destination = null;
        if (jndiLookupDestinations) {
            destination = (Destination) context.lookup(destinationName);
        } else {
            if (useQueueDestinations) {
                if (useTemporaryDestinations) {
                    destination = session.createTemporaryQueue();
                } else {
                    destination = session.createQueue(destinationName);
                }
            } else {
                if (useTemporaryDestinations) {
                    destination = session.createTemporaryTopic();
                } else {
                    destination = session.createTopic(destinationName);
                }
            }
        }

        MessageProducer producer = session.createProducer(destination);
        if (durable) {
            producer.setDeliveryMode(DeliveryMode.PERSISTENT);
        } else {
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        }

        int numMessagesToSend = useFinalControlMessage ? numMessages - 1 : numMessages;

        for (int i = 0; i < numMessagesToSend; i++) {
            String messageText = "Message " + i + " at " + new Date();
            if (bytesLength > -1) {
                byte[] messageTextBytes = messageText.getBytes(StandardCharsets.UTF_8);
                BytesMessage bytesMessage = session.createBytesMessage();
                bytesMessage.writeBytes(messageTextBytes);
                if (messageTextBytes.length < bytesLength) {
                    byte[] paddingBytes = new byte[bytesLength - messageTextBytes.length];
                    bytesMessage.writeBytes(paddingBytes);
                }
                if (messageGroupId != null) {
                    bytesMessage.setStringProperty("JMSXGroupID", messageGroupId);
                }
                LOGGER.info("Sending bytes message");
                producer.send(bytesMessage);
            } else {
                TextMessage textMessage = session.createTextMessage(messageText);
                if (messageGroupId != null) {
                    textMessage.setStringProperty("JMSXGroupID", messageGroupId);
                }
                LOGGER.info("Sending text message: " + messageText);
                producer.send(textMessage);
            }

            if (perMessageSleepMS > 0) {
                Thread.sleep(perMessageSleepMS);
            }
            if (transacted) {
                if ((i + 1) % batchSize == 0) {
                    session.commit();
                }
            }
        }
        if (useFinalControlMessage) {
            Message message = session.createMessage();
            if (messageGroupId != null) {
                message.setStringProperty("JMSXGroupID", messageGroupId);
            }
            LOGGER.info("Sending message");
            producer.send(message);
            if (transacted) {
                session.commit();
            }
        }
        producer.close();
    } catch (Exception ex) {
        LOGGER.error("ProducerTool hit exception: " + ex.getMessage(), ex);
    } finally {
        if (session != null) {
            try {
                session.close();
            } catch (JMSException e) {
                LOGGER.error("JMSException closing session", e);
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (JMSException e) {
                LOGGER.error("JMSException closing session", e);
            }
        }
    }
}