Package org.apache.agila.impl

Source Code of org.apache.agila.impl.JMSQueueServiceImpl

/*
* Copyright 2004 The Apache Software Foundation.
*
* 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.apache.agila.impl;
import java.util.Hashtable;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.apache.agila.engine.EngineMessage;
import org.apache.agila.engine.MessageProcessor;
import org.apache.agila.services.QueueService;

/**
* @version $Id: JMSQueueServiceImpl.java 38 2005-06-01 19:39:54Z chirino $
*/
public class JMSQueueServiceImpl implements QueueService, MessageListener {
    private final MessageProcessor mp;
    private MessageProducer producer;
    private MessageConsumer consumer;
    private Session session;

    /**
     * Constructor for JMSQueueServiceImpl
     * @param mp the MessageProcessor
     * @param env environment for JNDI lookup of 'ConnectionFactory' for JMS
     * @param queueName the name of the JMS queue to use
     * @throws NamingException
     * @throws JMSException
     */
    public JMSQueueServiceImpl(MessageProcessor mp, Hashtable env, String queueName) throws NamingException,
            JMSException {
        this.mp = mp;
        Context context = new InitialContext(env);
        ConnectionFactory fac = (ConnectionFactory) context.lookup("ConnectionFactory");
        Connection connection = fac.createConnection();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Destination destination = session.createQueue(queueName);
        consumer = session.createConsumer(destination);
        producer = session.createProducer(destination);
        connection.start();
    }

  
    /**
     * Enqueue an EngineMessage
     * @param em
     *
     */
    public void enqueue(EngineMessage em) {
        try {
            ObjectMessage msg = session.createObjectMessage(em);
            producer.send(msg);
        }
        catch (JMSException e) {
            e.printStackTrace();
            // TODO want to do something here
        }
    }

    /**
     * Unwraps a transport object to get the enclosed engine message This is necessary because the transport receiver
     * may be decoupled from the QueueService implementation, and this will isolate the transport from clients and other
     * services
     *
     * @param o the object
     * @return the unwraped engine message
     */
    public EngineMessage unwrap(Object o) {
        return (EngineMessage) o;
    }

    /**
     * MessageListener implementation
     * @param msg
     *
     */
    public void onMessage(Message msg) {
        try {
            EngineMessage en = (EngineMessage) ((ObjectMessage) msg).getObject();
            mp.processMessage(en);
        }
        catch (JMSException e) {
            e.printStackTrace();
        }
    }
}
TOP

Related Classes of org.apache.agila.impl.JMSQueueServiceImpl

TOP
Copyright © 2018 www.massapi.com. 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.