org.springframework.amqp.rabbit.connection.RabbitAccessor.java Source code

Java tutorial

Introduction

Here is the source code for org.springframework.amqp.rabbit.connection.RabbitAccessor.java

Source

/*
 * Copyright 2002-2010 the original author or authors.
 *
 * 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.springframework.amqp.rabbit.connection;

import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;

import com.rabbitmq.client.Channel;

/**
 * @author Mark Fisher
 * @author Dave Syer
 */
public abstract class RabbitAccessor implements InitializingBean {

    /** Logger available to subclasses */
    protected final Log logger = LogFactory.getLog(getClass());

    private volatile ConnectionFactory connectionFactory;

    private volatile boolean transactional;

    public boolean isChannelTransacted() {
        return this.transactional;
    }

    /**
     * Flag to indicate that channels created by this component will be transactional.
     *
     * @param transactional the flag value to set
     */
    public void setChannelTransacted(boolean transactional) {
        this.transactional = transactional;
    }

    /**
     * Set the ConnectionFactory to use for obtaining RabbitMQ {@link Connection Connections}.
     */
    public void setConnectionFactory(ConnectionFactory connectionFactory) {
        this.connectionFactory = connectionFactory;
    }

    /**
     * Return the ConnectionFactory that this accessor uses for obtaining RabbitMQ {@link Connection Connections}.
     */
    public ConnectionFactory getConnectionFactory() {
        return this.connectionFactory;
    }

    public void afterPropertiesSet() {
        Assert.notNull(this.connectionFactory, "ConnectionFactory is required");
    }

    /**
     * Create a RabbitMQ Connection via this template's ConnectionFactory and its host and port values.
     * @return the new RabbitMQ Connection
     * @throws IOException if thrown by RabbitMQ API methods
     * @see ConnectionFactory#createConnection
     */
    protected Connection createConnection() throws IOException {
        return this.connectionFactory.createConnection();
    }

    /**
     * Fetch an appropriate Connection from the given RabbitResourceHolder.
     *
     * @param holder the RabbitResourceHolder
     * @return an appropriate Connection fetched from the holder, or <code>null</code> if none found
     */
    protected Connection getConnection(RabbitResourceHolder holder) {
        return holder.getConnection();
    }

    /**
     * Fetch an appropriate Channel from the given RabbitResourceHolder.
     *
     * @param holder the RabbitResourceHolder
     * @return an appropriate Channel fetched from the holder, or <code>null</code> if none found
     */
    protected Channel getChannel(RabbitResourceHolder holder) {
        return holder.getChannel();
    }

    protected RabbitResourceHolder getTransactionalResourceHolder() {
        RabbitResourceHolder holder = ConnectionFactoryUtils.getTransactionalResourceHolder(this.connectionFactory,
                isChannelTransacted());
        return holder;
    }

    protected RuntimeException convertRabbitAccessException(Exception ex) {
        return RabbitUtils.convertRabbitAccessException(ex);
    }

}