001/*
002 * Copyright 2002-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.jms.core;
018
019import javax.jms.JMSException;
020import javax.jms.MessageProducer;
021import javax.jms.Session;
022
023import org.springframework.lang.Nullable;
024
025/**
026 * Callback for sending a message to a JMS destination.
027 *
028 * <p>To be used with {@link JmsTemplate}'s callback methods that take a
029 * {@link ProducerCallback} argument, often implemented as an anonymous
030 * inner class or as a lambda expression.
031 *
032 * <p>The typical implementation will perform multiple operations on the
033 * supplied JMS {@link Session} and {@link MessageProducer}.
034 *
035 * @author Mark Pollack
036 * @since 1.1
037 * @param <T> the result type
038 * @see JmsTemplate#execute(ProducerCallback)
039 * @see JmsTemplate#execute(javax.jms.Destination, ProducerCallback)
040 * @see JmsTemplate#execute(String, ProducerCallback)
041 */
042@FunctionalInterface
043public interface ProducerCallback<T> {
044
045        /**
046         * Perform operations on the given {@link Session} and {@link MessageProducer}.
047         * <p>The message producer is not associated with any destination unless
048         * when specified in the JmsTemplate call.
049         * @param session the JMS {@code Session} object to use
050         * @param producer the JMS {@code MessageProducer} object to use
051         * @return a result object from working with the {@code Session}, if any
052         * (or {@code null} if none)
053         * @throws javax.jms.JMSException if thrown by JMS API methods
054         */
055        @Nullable
056        T doInJms(Session session, MessageProducer producer) throws JMSException;
057
058}