Package org.codehaus.activemq.ra

Source Code of org.codehaus.activemq.ra.ActiveMQBaseEndpointWorker

/**
*
* Copyright 2004 Hiram Chirino
*
* 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.codehaus.activemq.ra;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.activemq.ActiveMQSession;

import javax.jms.Connection;
import javax.jms.ConnectionConsumer;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.resource.ResourceException;
import javax.resource.spi.endpoint.MessageEndpointFactory;
import javax.resource.spi.work.WorkException;
import javax.resource.spi.work.WorkManager;
import java.lang.reflect.Method;

/**
* @version $Revision: 1.5 $ $Date: 2004/11/20 05:49:51 $
*/
public abstract class ActiveMQBaseEndpointWorker {

    private static final Log log = LogFactory.getLog(ActiveMQBaseEndpointWorker.class);
    public static final Method ON_MESSAGE_METHOD;

    static {
        try {
            ON_MESSAGE_METHOD = MessageListener.class.getMethod("onMessage", new Class[]{Message.class});
        }
        catch (Exception e) {
            throw new ExceptionInInitializerError(e);
        }
    }

    protected ActiveMQResourceAdapter adapter;
    protected ActiveMQEndpointActivationKey endpointActivationKey;
    protected MessageEndpointFactory endpointFactory;
    protected WorkManager workManager;
    protected boolean transacted;

    /**
     * @param s
     */
    public static void safeClose(Session s) {
        try {
            if (s != null) {
                s.close();
            }
        }
        catch (JMSException e) {
        }
    }

    /**
     * @param cc
     */
    public static void safeClose(ConnectionConsumer cc) {
        try {
            if (cc != null) {
                cc.close();
            }
        }
        catch (JMSException e) {
        }
    }

    public ActiveMQBaseEndpointWorker(ActiveMQResourceAdapter adapter, ActiveMQEndpointActivationKey key) throws ResourceException {
        this.endpointActivationKey = key;
        this.adapter = adapter;
        this.endpointFactory = endpointActivationKey.getMessageEndpointFactory();
        this.workManager = adapter.getBootstrapContext().getWorkManager();
        try {
            this.transacted = endpointFactory.isDeliveryTransacted(ON_MESSAGE_METHOD);
        }
        catch (NoSuchMethodException e) {
            throw new ResourceException("Endpoint does not implement the onMessage method.");
        }
    }

    public abstract void start() throws WorkException, ResourceException;

    public abstract void stop() throws InterruptedException;


    /**
     * Factory method to create a new JMS session instance
     *
     * @return a newly created JMS session
     * @throws javax.jms.JMSException
     */
    protected ActiveMQSession createSession() throws JMSException {
        int acknowledge = (transacted) ? Session.SESSION_TRANSACTED :
                            endpointActivationKey.getActivationSpec().getAcknowledgeModeForSession();
        ActiveMQSession session = (ActiveMQSession) getPhysicalConnection().createSession(transacted, acknowledge);
        return session;
    }

    protected Connection getPhysicalConnection() throws JMSException {
        return adapter.getPhysicalConnection(transacted);
    }
}
TOP

Related Classes of org.codehaus.activemq.ra.ActiveMQBaseEndpointWorker

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.