org.symplify.runtime.jms.JMSContextImpl.java Source code

Java tutorial

Introduction

Here is the source code for org.symplify.runtime.jms.JMSContextImpl.java

Source

/*
 * Copyright 2010 Symplify.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package org.symplify.runtime.jms;

import javax.jms.Connection;
import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.symplify.runtime.Channel;
import org.symplify.runtime.ClientContext;
import org.symplify.runtime.ControlToken;
import org.symplify.runtime.Message;
import org.symplify.runtime.command.GetServiceCommand;
import org.symplify.runtime.common.ControlTokenImpl;
import org.symplify.runtime.common.MessageImpl;
import org.symplify.runtime.common.NamedChannelImpl;
import org.symplify.runtime.common.UniqueChannelImpl;
import org.symplify.runtime.services.Definitions;

public class JMSContextImpl implements ClientContext, ExceptionListener {

    public JMSContextImpl() {
        initialize();
    }

    protected void initialize() {
        try {
            ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(m_user, m_password, m_url);
            m_connection = connectionFactory.createConnection();
            m_connection.setExceptionListener(this);
            m_connection.start();

            m_session = m_connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected javax.jms.Queue getQueue(Channel channel) {
        javax.jms.Queue ret = null;

        try {
            ret = m_session.createQueue(channel.getName());
        } catch (Exception e) {
        }

        return (ret);
    }

    public Channel getService(String serviceName) throws Exception {
        Channel ret = null;

        if (serviceName.equals(Definitions.REGISTRY_SERVICE)) {
            ret = REGISTRY_CHANNEL;
        } else {
            //GetServiceCommand command=new GetServiceCommand(serviceName);
            GetServiceCommand regMesg = new GetServiceCommand(serviceName);

            //Message regMesg=createMessage();
            //regMesg.setContent(command);

            Channel replyTo = createChannel();
            regMesg.setReplyChannel(replyTo);

            send(REGISTRY_CHANNEL, regMesg);

            Message reply = receive(replyTo, 5000);

            if (reply != null && reply.getContent() instanceof Channel) {
                ret = (Channel) reply.getContent();
            }
        }

        return (ret);
    }

    public void send(Channel channel, Message message) throws Exception {
        javax.jms.Queue queue = getQueue(channel);

        if (queue != null) {
            //queue.put(message);
            javax.jms.MessageProducer producer = m_session.createProducer(queue);

            javax.jms.Message m = m_session.createObjectMessage(message);

            producer.send(m); //, 0, 0, message.getTimeToLive());

            producer.close();

        } else {
            throw new Exception("Unable to find queue for channel");
        }
    }

    public Message receive(Channel channel, long timeout) throws Exception {
        Message ret = null;
        javax.jms.Queue queue = getQueue(channel);

        if (queue != null) {
            javax.jms.MessageConsumer consumer = m_session.createConsumer(queue);

            javax.jms.Message m = consumer.receive(timeout);

            consumer.close();

            if (m instanceof javax.jms.ObjectMessage) {
                ret = (Message) ((javax.jms.ObjectMessage) m).getObject();
            }
        } else if (m_closed == false) {
            throw new Exception("Unable to find queue for channel");
        }

        if (logger.isDebugEnabled()) {
            logger.debug("Receive (timeout " + timeout + ") on channel " + channel + " = " + ret);
        }

        return (ret);
    }

    public Message receiveNoWait(Channel channel) throws Exception {
        Message ret = null;
        javax.jms.Queue queue = getQueue(channel);

        if (queue != null) {
            javax.jms.MessageConsumer consumer = m_session.createConsumer(queue);

            javax.jms.Message m = consumer.receive(5);

            if (m instanceof javax.jms.ObjectMessage) {
                ret = (Message) ((javax.jms.ObjectMessage) m).getObject();
            }

            consumer.close();

        } else {
            throw new Exception("Unable to find queue for channel");
        }

        if (logger.isDebugEnabled()) {
            logger.debug("Receive (no wait) on channel " + channel + " = " + ret);
        }

        return (ret);
    }

    public Message peek(Channel channel) throws Exception {
        Message ret = null;
        javax.jms.Queue queue = getQueue(channel);

        if (queue != null) {
            javax.jms.QueueBrowser browser = m_session.createBrowser(queue);

            if (browser.getEnumeration().hasMoreElements()) {
                javax.jms.Message m = (javax.jms.Message) browser.getEnumeration().nextElement();

                if (m instanceof javax.jms.ObjectMessage) {
                    ret = (Message) ((javax.jms.ObjectMessage) m).getObject();
                }
            }

            browser.close();

        } else {
            throw new Exception("Unable to find queue for channel");
        }

        if (logger.isDebugEnabled()) {
            logger.debug("Peek on channel " + channel + " = " + ret);
        }

        return (ret);
    }

    public Message createMessage() {
        return (new MessageImpl());
    }

    public Channel createChannel() {
        UniqueChannelImpl ret = new UniqueChannelImpl();
        return (ret);
    }

    public void removeChannel(Channel ch) {
        // Remove resources allocated to the channel
    }

    public ControlToken createControlToken() {
        return (new ControlTokenImpl());
    }

    public synchronized void onException(JMSException ex) {
    }

    public void close() throws Exception {
        if (logger.isDebugEnabled()) {
            logger.debug("Closing context: " + this);
        }

        m_closed = true;

        m_session.close();

        m_connection.close();
    }

    private static Log logger = LogFactory.getLog(JMSContextImpl.class);

    private static final Channel REGISTRY_CHANNEL = new NamedChannelImpl(Definitions.REGISTRY_SERVICE);

    private String m_user = ActiveMQConnection.DEFAULT_USER;
    private String m_password = ActiveMQConnection.DEFAULT_PASSWORD;
    private String m_url = ActiveMQConnection.DEFAULT_BROKER_URL;

    private javax.jms.Session m_session = null;
    private Connection m_connection = null;
    private boolean m_closed = false;
}