Package org.apache.activemq

Examples of org.apache.activemq.ActiveMQSession


        // Create consumer on separate connection
        ActiveMQConnection consumerConnection = (ActiveMQConnection) cf.createConnection();
        consumerConnection.setMessagePrioritySupported(true);
        consumerConnection.start();
        final ActiveMQSession consumerSession = (ActiveMQSession) consumerConnection.createSession(true,
                Session.SESSION_TRANSACTED);
        consumer = (ActiveMQMessageConsumer) consumerSession.createConsumer(dest);

        // Produce X number of messages with a session commit after each message
        Random random = new Random();
        for (int i = 0; i < messageCount; ++i) {

            Message message = producerSession.createTextMessage("Test message #" + i);
            producer.send(message, DeliveryMode.PERSISTENT, random.nextInt(10), 45*1000);
            producerSession.commit();
        }
        producer.close();

        // ***************************************************
        // If we create the consumer here instead of above, the
        // the messages will be consumed in priority order
        // ***************************************************
        //consumer = (ActiveMQMessageConsumer) consumerSession.createConsumer(dest);

        // Consume all of the messages we produce using a listener.
        // Don't exit until we get all the messages.
        final CountDownLatch latch = new CountDownLatch(messageCount);
        final StringBuffer failureMessage = new StringBuffer();
        consumer.setMessageListener(new MessageListener() {
            int lowestPrioritySeen = 10;

            boolean firstMessage = true;

            public void onMessage(Message msg) {
                try {

                    int currentPriority = msg.getJMSPriority();
                    LOG.debug(currentPriority + "<=" + lowestPrioritySeen);

                    // Ignore the first message priority since it is prefetched
                    // and is out of order by design
                    if (firstMessage == true) {
                        firstMessage = false;
                        LOG.debug("Ignoring first message since it was prefetched");

                    } else {

                        // Verify that we never see a priority higher than the
                        // lowest
                        // priority seen
                        if (lowestPrioritySeen > currentPriority) {
                            lowestPrioritySeen = currentPriority;
                        }
                        if (lowestPrioritySeen < currentPriority) {
                            failureMessage.append("Incorrect priority seen (Lowest Priority = " + lowestPrioritySeen
                                    + " Current Priority = " + currentPriority + ")"
                                    + System.getProperty("line.separator"));
                        }
                    }

                } catch (JMSException e) {
                    e.printStackTrace();
                } finally {
                    latch.countDown();
                    LOG.debug("Messages remaining = " + latch.getCount());
                }
            }
        });

        latch.await();
        consumer.close();

        // Cleanup producer resources
        producerSession.close();
        producerConnection.stop();
        producerConnection.close();

        // Cleanup consumer resources
        consumerSession.close();
        consumerConnection.stop();
        consumerConnection.close();

        // Report the failure if found
        if (failureMessage.length() > 0) {
View Full Code Here


        public void send() throws Exception{
            ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, uri);
            ActiveMQConnection connection = (ActiveMQConnection)connectionFactory.createConnection();
            connection.start();

            ActiveMQSession session = (ActiveMQSession)connection.createSession(transacted, Session.AUTO_ACKNOWLEDGE);
            Destination destination = session.createTopic("topic2");
            MessageProducer producer = session.createProducer(destination);
            producer.setDeliveryMode(DeliveryMode.PERSISTENT);
            for(int i=0;i<100;i++){
                TextMessage om=session.createTextMessage("hello from producer");
                producer.send(om);
            }
            producer.close();
            session.close();
            connection.close();
        }
View Full Code Here

        waitForBridgeFormation(b, 1, 0);

        ActiveMQConnectionFactory sendFactory = createConnectionFactory(a);
        ActiveMQConnection sendConnection = createConnection(sendFactory);

        ActiveMQSession sendSession = (ActiveMQSession)sendConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer producer = sendSession.createProducer(sendQ);
        ActiveMQTempQueue realReplyQ = (ActiveMQTempQueue) sendSession.createTemporaryQueue();
        TextMessage message = sendSession.createTextMessage("1");
        message.setJMSReplyTo(realReplyQ);
        producer.send(message);
        LOG.info("request sent");

        // responder
        ActiveMQConnectionFactory consumerFactory = createConnectionFactory(b);
        ActiveMQConnection consumerConnection = createConnection(consumerFactory);

        ActiveMQSession consumerSession = (ActiveMQSession)consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageConsumer consumer = consumerSession.createConsumer(sendQ);
        TextMessage received = (TextMessage) consumer.receive(receiveTimeout);
        assertNotNull("got request from sender ok", received);

        LOG.info("got request, sending reply");

        MessageProducer consumerProducer = consumerSession.createProducer(received.getJMSReplyTo());
        consumerProducer.send(consumerSession.createTextMessage("got " + received.getText()));
        // temp dest on reply broker tied to this connection, setOptimizedDispatch=true ensures
        // message gets delivered before destination is removed
        consumerConnection.close();

        // reply consumer
View Full Code Here

        public static boolean recover(final ActiveMQResourceManager rm) throws IOException {
            if (isRecoverable(rm)) {
                try {
                    final ActiveMQConnectionFactory connFactory = (ActiveMQConnectionFactory) rm.getConnectionFactory();
                    ActiveMQConnection activeConn = (ActiveMQConnection)connFactory.createConnection();
                    final ActiveMQSession session = (ActiveMQSession)activeConn.createSession(true, Session.SESSION_TRANSACTED);
                    NamedXAResource namedXaResource = new WrapperNamedXAResource(session.getTransactionContext(), rm.getResourceName());

                    RecoverableTransactionManager rtxManager = (RecoverableTransactionManager) rm.getTransactionManager();
                    rtxManager.registerNamedXAResourceFactory(new NamedXAResourceFactory() {

                        @Override
                        public String getName() {
                            return rm.getResourceName();
                        }

                        @Override
                        public NamedXAResource getNamedXAResource() throws SystemException {
                            try {
                                final ActiveMQConnection activeConn = (ActiveMQConnection)connFactory.createConnection();
                                final ActiveMQSession session = (ActiveMQSession)activeConn.createSession(true, Session.SESSION_TRANSACTED);
                                activeConn.start();
                                LOGGER.debug("new namedXAResource's connection: " + activeConn);

                                return new ConnectionAndWrapperNamedXAResource(session.getTransactionContext(), getName(), activeConn);
                            } catch (Exception e) {
                                SystemException se =  new SystemException("Failed to create ConnectionAndWrapperNamedXAResource, " + e.getLocalizedMessage());
                                se.initCause(e);
                                LOGGER.error(se.getLocalizedMessage(), se);
                                throw se;
View Full Code Here

                    session.getInternalSession().close();
                }

                @Override
                public PooledSession makeObject(SessionKey key) throws Exception {
                    ActiveMQSession session = (ActiveMQSession)
                            ConnectionPool.this.connection.createSession(key.isTransacted(), key.getAckMode());
                    return new PooledSession(key, session, sessionPool);
                }

                @Override
View Full Code Here

        public void run() {

            ActiveMQConnectionFactory connectionFactory = null;
            ActiveMQConnection connection = null;
            ActiveMQSession session = null;
            Destination destination = null;

            try {
                LOG.info("Started TestProducer for destination (" + destinationName + ")");

                connectionFactory = new ActiveMQConnectionFactory(jmsConnectionURI);
                connection = (ActiveMQConnection) connectionFactory.createConnection();
                connection.setCopyMessageOnSend(false);
                connection.start();
                session = (ActiveMQSession) connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

                destination = session.createQueue(this.destinationName);

                // Create a MessageProducer from the Session to the Topic or Queue
                ActiveMQMessageProducer producer = (ActiveMQMessageProducer) session.createProducer(destination);
                producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

                for (int i = 0; i < (producerMessages); i++) {
                    TextMessage message = (TextMessage) session.createTextMessage();
                    message.setLongProperty("TestTime", (System.currentTimeMillis()));
                    try {
                        producer.send(message);
                        LOG.info("Producer (" + destinationName + ")\n" + message.getJMSMessageID() + " = sent messageId\n");

                        latch.countDown();
                        LOG.info(" Latch count  " + latch.getCount());
                        LOG.info("Producer message list size = " + messageList.keySet().size());
                        messageList.put(message.getJMSMessageID(), new AtomicInteger(0));
                        LOG.info("Producer message list size = " + messageList.keySet().size());

                    } catch (Exception deeperException) {
                        LOG.info("Producer for destination (" + destinationName + ") Caught: " + deeperException);
                    }

                    Thread.sleep(1000);
                }

                LOG.info("Finished TestProducer for destination (" + destinationName + ")");

            } catch (Exception e) {
                LOG.error("Terminating TestProducer(" + destinationName + ")Caught: " + e);
            } finally {
                try {
                    if (session != null) {
                        session.close();
                    }
                    if (connection != null) {
                        connection.close();
                    }
                } catch (Exception e) {
View Full Code Here

            // check if we should use a blob message here
            if (useBlob) {
                MimeMessage mm = mail.getMessage();
                MimeMessage wrapper = mm;
               
                ActiveMQSession amqSession = getAMQSession(session);
               
                if (wrapper instanceof MimeMessageCopyOnWriteProxy) {
                    wrapper = ((MimeMessageCopyOnWriteProxy)mm).getWrappedMessage();
                }
                BlobMessage blobMessage = null;
                if (wrapper instanceof MimeMessageWrapper) {
                    URL blobUrl = (URL) mail.getAttribute(JAMES_BLOB_URL);
                    String fromQueue = (String) mail.getAttribute(JAMES_QUEUE_NAME);
                    MimeMessageWrapper mwrapper = (MimeMessageWrapper) wrapper;

                    if (blobUrl != null && fromQueue != null && fromQueue.equals(queuename) && mwrapper.isModified() == false ) {
                        // the message content was not changed so don't need to upload it again and can just point to the url
                        blobMessage = amqSession.createBlobMessage(blobUrl);
                   
                        // thats important so we don't delete the blob file after complete the processing!
                        mail.setAttribute(JAMES_REUSE_BLOB_URL, true);
                   
                    }

                }
                if (blobMessage == null) {
                    // just use the MimeMessageInputStream which can read every MimeMessage implementation
                    blobMessage = amqSession.createBlobMessage(new MimeMessageInputStream(wrapper));
                }
           
                // store the queue name in the props
                props.put(JAMES_QUEUE_NAME, queuename);
View Full Code Here

     * @param session
     * @return amqSession
     * @throws JMSException
     */
    protected ActiveMQSession getAMQSession(Session session) throws JMSException {
        ActiveMQSession amqSession;
       
        if (session instanceof SessionProxy) {
            // handle Springs CachingConnectionFactory
            amqSession = (ActiveMQSession) ((SessionProxy) session).getTargetSession();
        } else {
View Full Code Here

    }

    protected void useConnectionWithBlobMessage(Connection connection) throws Exception {
        connection.setClientID(clientID);
        connection.start();
        ActiveMQSession session = (ActiveMQSession) connection.createSession(transacted, authMode);
        destination = createDestination();
        MessageProducer producer = session.createProducer(destination);
        for (int i = 0; i < MESSAGE_COUNT; i++) {
            BlobMessage message = session.createBlobMessage(new URL("http://foo.bar/test"));
            message.setIntProperty("counter", i);
            message.setJMSCorrelationID("MyCorrelationID");
            message.setJMSReplyTo(new ActiveMQQueue("MyReplyTo"));
            message.setJMSType("MyType");
            message.setJMSPriority(5);
View Full Code Here

    }

    protected void useConnectionWithByteMessage(Connection connection) throws Exception {
        connection.setClientID(clientID);
        connection.start();
        ActiveMQSession session = (ActiveMQSession) connection.createSession(transacted, authMode);
        destination = createDestination();
        MessageProducer producer = session.createProducer(destination);
        for (int i = 0; i < MESSAGE_COUNT; i++) {
            BytesMessage message = session.createBytesMessage();
            message.writeBytes(("Message: " + i).getBytes());
            message.setIntProperty("counter", i);
            message.setJMSCorrelationID("MyCorrelationID");
            message.setJMSReplyTo(new ActiveMQQueue("MyReplyTo"));
            message.setJMSType("MyType");
View Full Code Here

TOP

Related Classes of org.apache.activemq.ActiveMQSession

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.