001/* 002 * Copyright 2006-2007 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.batch.item.jms; 018 019import javax.jms.JMSException; 020import javax.jms.Message; 021 022import org.springframework.batch.item.UnexpectedInputException; 023import org.springframework.retry.interceptor.MethodArgumentsKeyGenerator; 024 025/** 026 * A {@link MethodArgumentsKeyGenerator} for JMS 027 * 028 * @author Dave Syer 029 * 030 */ 031public class JmsMethodArgumentsKeyGenerator implements MethodArgumentsKeyGenerator { 032 033 /** 034 * If the message is a {@link Message} then returns the JMS message ID. 035 * Otherwise just return the first argument. 036 * 037 * @see org.springframework.retry.interceptor.MethodArgumentsKeyGenerator#getKey(Object[]) 038 * 039 * @throws UnexpectedInputException if the JMS id cannot be determined from 040 * a JMS Message 041 * @throws IllegalArgumentException if the arguments are empty 042 */ 043 @Override 044 public Object getKey(Object[] items) { 045 for (Object item : items) { 046 if (item instanceof Message) { 047 try { 048 return ((Message) item).getJMSMessageID(); 049 } 050 catch (JMSException e) { 051 throw new UnexpectedInputException("Could not extract message ID", e); 052 } 053 } 054 } 055 if (items.length == 0) { 056 throw new IllegalArgumentException( 057 "Method parameters are empty. The key generator cannot determine a unique key."); 058 } 059 return items[0]; 060 } 061 062}