| 1 | /* |
| 2 | * Copyright 2006-2007 the original author or authors. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.springframework.batch.item.jms; |
| 17 | |
| 18 | import org.apache.commons.logging.Log; |
| 19 | import org.apache.commons.logging.LogFactory; |
| 20 | import org.springframework.retry.interceptor.MethodInvocationRecoverer; |
| 21 | import org.springframework.jms.JmsException; |
| 22 | import org.springframework.jms.core.JmsOperations; |
| 23 | |
| 24 | /** |
| 25 | * @author Dave Syer |
| 26 | * |
| 27 | */ |
| 28 | public class JmsMethodInvocationRecoverer<T> implements MethodInvocationRecoverer<T> { |
| 29 | |
| 30 | protected Log logger = LogFactory.getLog(getClass()); |
| 31 | |
| 32 | private JmsOperations jmsTemplate; |
| 33 | |
| 34 | /** |
| 35 | * Setter for jms template. |
| 36 | * |
| 37 | * @param jmsTemplate a {@link JmsOperations} instance |
| 38 | */ |
| 39 | public void setJmsTemplate(JmsOperations jmsTemplate) { |
| 40 | this.jmsTemplate = jmsTemplate; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Send one message per item in the arguments list using the default destination of |
| 45 | * the jms template. If the recovery is successful null is returned. |
| 46 | * |
| 47 | * @see org.springframework.retry.interceptor.MethodInvocationRecoverer#recover(Object[], |
| 48 | * Throwable) |
| 49 | */ |
| 50 | @Override |
| 51 | public T recover(Object[] items, Throwable cause) { |
| 52 | try { |
| 53 | for (Object item : items) { |
| 54 | jmsTemplate.convertAndSend(item); |
| 55 | } |
| 56 | return null; |
| 57 | } |
| 58 | catch (JmsException e) { |
| 59 | logger.error("Could not recover because of JmsException.", e); |
| 60 | throw e; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | } |