001/*
002 * Copyright 2006-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 */
016package org.springframework.batch.item.jms;
017
018import org.apache.commons.logging.Log;
019import org.apache.commons.logging.LogFactory;
020
021import org.springframework.lang.Nullable;
022import org.springframework.retry.interceptor.MethodInvocationRecoverer;
023import org.springframework.jms.JmsException;
024import org.springframework.jms.core.JmsOperations;
025
026/**
027 * @author Dave Syer
028 * @author Mahmoud Ben Hassine
029 * 
030 */
031public class JmsMethodInvocationRecoverer<T> implements MethodInvocationRecoverer<T> {
032
033        protected Log logger = LogFactory.getLog(getClass());
034
035        private JmsOperations jmsTemplate;
036
037        /**
038         * Setter for jms template.
039         * 
040         * @param jmsTemplate a {@link JmsOperations} instance
041         */
042        public void setJmsTemplate(JmsOperations jmsTemplate) {
043                this.jmsTemplate = jmsTemplate;
044        }
045
046        /**
047         * Send one message per item in the arguments list using the default destination of
048         * the jms template. If the recovery is successful {@code null} is returned.
049         * 
050         * @see org.springframework.retry.interceptor.MethodInvocationRecoverer#recover(Object[],
051         * Throwable)
052         */
053    @Override
054        @Nullable
055        public T recover(Object[] items, Throwable cause) {
056                try {
057                        for (Object item : items) {
058                                jmsTemplate.convertAndSend(item);
059                        }
060                        return null;
061                }
062                catch (JmsException e) {
063                        logger.error("Could not recover because of JmsException.", e);
064                        throw e;
065                }
066        }
067
068}