List of usage examples for org.springframework.amqp.rabbit.connection RabbitResourceHolder getChannel
@Nullable
public Channel getChannel()
From source file:org.kurento.rabbitmq.RabbitTemplate.java
private <T> T doExecute(ChannelCallback<T> action) { Assert.notNull(action, "Callback object must not be null"); RabbitResourceHolder resourceHolder = getTransactionalResourceHolder(); Channel channel = resourceHolder.getChannel(); if (this.confirmCallback != null || this.returnCallback != null) { addListener(channel);//from w w w . jav a2s .c o m } try { if (logger.isDebugEnabled()) { logger.debug("Executing callback on RabbitMQ Channel: " + channel); } return action.doInRabbit(channel); } catch (Exception ex) { if (isChannelLocallyTransacted(channel)) { resourceHolder.rollbackAll(); } throw convertRabbitAccessException(ex); } finally { ConnectionFactoryUtils.releaseResources(resourceHolder); } }
From source file:org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils.java
/** * Obtain a RabbitMQ Channel that is synchronized with the current * transaction, if any./*w ww . j av a2 s . co m*/ * * @param connectionFactory * the ConnectionFactory to obtain a Channel for * @param synchedLocalTransactionAllowed * whether to allow for a local RabbitMQ transaction that is * synchronized with a Spring-managed transaction (where the main * transaction might be a JDBC-based one for a specific * DataSource, for example), with the RabbitMQ transaction * committing right after the main transaction. If not allowed, * the given ConnectionFactory needs to handle transaction * enlistment underneath the covers. * @return the transactional Channel, or <code>null</code> if none found */ public static RabbitResourceHolder getTransactionalResourceHolder(final ConnectionFactory connectionFactory, final boolean synchedLocalTransactionAllowed) { RabbitResourceHolder holder = doGetTransactionalResourceHolder(connectionFactory, new ResourceFactory() { public Channel getChannel(RabbitResourceHolder holder) { return holder.getChannel(); } public Connection getConnection(RabbitResourceHolder holder) { return holder.getConnection(); } public Connection createConnection() throws IOException { return connectionFactory.createConnection(); } public Channel createChannel(Connection con) throws IOException { return con.createChannel(synchedLocalTransactionAllowed); } public boolean isSynchedLocalTransactionAllowed() { return synchedLocalTransactionAllowed; } }); return holder; }
From source file:org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils.java
public static void releaseResources(RabbitResourceHolder resourceHolder) { if (resourceHolder == null || resourceHolder.isSynchronizedWithTransaction()) { return;/* w w w .j a v a2 s .co m*/ } RabbitUtils.closeChannel(resourceHolder.getChannel()); RabbitUtils.closeConnection(resourceHolder.getConnection()); }
From source file:org.springframework.amqp.rabbit.connection.RabbitAccessor.java
/** * 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 *///from w w w. j av a 2s. com protected Channel getChannel(RabbitResourceHolder holder) { return holder.getChannel(); }
From source file:org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.java
/** * Invoke the specified listener as Spring ChannelAwareMessageListener, exposing a new Rabbit Session (potentially * with its own transaction) to the listener if demanded. * @param listener the Spring ChannelAwareMessageListener to invoke * @param channel the Rabbit Channel to operate on * @param message the received Rabbit Message * @throws Exception if thrown by Rabbit API methods or listener itself. * <p>// www . j av a 2 s . c o m * Exception thrown from listener will be wrapped to {@link ListenerExecutionFailedException}. * @see ChannelAwareMessageListener * @see #setExposeListenerChannel(boolean) */ protected void doInvokeListener(ChannelAwareMessageListener listener, Channel channel, Message message) throws Exception { RabbitResourceHolder resourceHolder = null; Channel channelToUse = channel; boolean boundHere = false; try { if (!isExposeListenerChannel()) { // We need to expose a separate Channel. resourceHolder = getTransactionalResourceHolder(); channelToUse = resourceHolder.getChannel(); /* * If there is a real transaction, the resource will have been bound; otherwise * we need to bind it temporarily here. Any work done on this channel * will be committed in the finally block. */ if (isChannelLocallyTransacted() && !TransactionSynchronizationManager.isActualTransactionActive()) { resourceHolder.setSynchronizedWithTransaction(true); TransactionSynchronizationManager.bindResource(this.getConnectionFactory(), resourceHolder); boundHere = true; } } else { // if locally transacted, bind the current channel to make it available to RabbitTemplate if (isChannelLocallyTransacted()) { RabbitResourceHolder localResourceHolder = new RabbitResourceHolder(channelToUse, false); localResourceHolder.setSynchronizedWithTransaction(true); TransactionSynchronizationManager.bindResource(this.getConnectionFactory(), localResourceHolder); boundHere = true; } } // Actually invoke the message listener... try { listener.onMessage(message, channelToUse); } catch (Exception e) { throw wrapToListenerExecutionFailedExceptionIfNeeded(e, message); } } finally { if (resourceHolder != null && boundHere) { // so the channel exposed (because exposeListenerChannel is false) will be closed resourceHolder.setSynchronizedWithTransaction(false); } ConnectionFactoryUtils.releaseResources(resourceHolder); if (boundHere) { // unbind if we bound TransactionSynchronizationManager.unbindResource(this.getConnectionFactory()); if (!isExposeListenerChannel() && isChannelLocallyTransacted()) { /* * commit the temporary channel we exposed; the consumer's channel * will be committed later. Note that when exposing a different channel * when there's no transaction manager, the exposed channel is committed * on each message, and not based on txSize. */ RabbitUtils.commitIfNecessary(channelToUse); } } } }