View Javadoc

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  
17  package org.springframework.batch.item.jms;
18  
19  import javax.jms.JMSException;
20  import javax.jms.Message;
21  
22  import org.springframework.batch.item.UnexpectedInputException;
23  import org.springframework.retry.interceptor.MethodArgumentsKeyGenerator;
24  
25  /**
26   * A {@link MethodArgumentsKeyGenerator} for JMS 
27   * 
28   * @author Dave Syer
29   * 
30   */
31  public class JmsMethodArgumentsKeyGenerator implements MethodArgumentsKeyGenerator {
32  
33  	/**
34  	 * If the message is a {@link Message} then returns the JMS message ID.
35  	 * Otherwise just return the first argument.
36  	 * 
37  	 * @see org.springframework.retry.interceptor.MethodArgumentsKeyGenerator#getKey(Object[])
38  	 * 
39  	 * @throws UnexpectedInputException if the JMS id cannot be determined from
40  	 * a JMS Message
41  	 * @throws IllegalArgumentException if the arguments are empty
42  	 */
43      @Override
44  	public Object getKey(Object[] items) {
45  		for (Object item : items) {
46  			if (item instanceof Message) {
47  				try {
48  					return ((Message) item).getJMSMessageID();
49  				}
50  				catch (JMSException e) {
51  					throw new UnexpectedInputException("Could not extract message ID", e);
52  				}
53  			}
54  		}
55  		if (items.length == 0) {
56  			throw new IllegalArgumentException(
57  					"Method parameters are empty.  The key generator cannot determine a unique key.");
58  		}
59  		return items[0];
60  	}
61  
62  }