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.Message;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.springframework.batch.item.ItemReader;
24  import org.springframework.beans.factory.InitializingBean;
25  import org.springframework.jms.core.JmsOperations;
26  import org.springframework.jms.core.JmsTemplate;
27  import org.springframework.util.Assert;
28  
29  /**
30   * An {@link ItemReader} for JMS using a {@link JmsTemplate}. The template
31   * should have a default destination, which will be used to provide items in
32   * {@link #read()}.<br/>
33   * <br/>
34   * 
35   * The implementation is thread safe after its properties are set (normal
36   * singleton behavior).
37   * 
38   * @author Dave Syer
39   * 
40   */
41  public class JmsItemReader<T> implements ItemReader<T>, InitializingBean {
42  
43  	protected Log logger = LogFactory.getLog(getClass());
44  
45  	protected Class<? extends T> itemType;
46  
47  	protected JmsOperations jmsTemplate;
48  
49  	/**
50  	 * Setter for jms template.
51  	 * 
52  	 * @param jmsTemplate a {@link JmsOperations} instance
53  	 */
54  	public void setJmsTemplate(JmsOperations jmsTemplate) {
55  		this.jmsTemplate = jmsTemplate;
56  		if (jmsTemplate instanceof JmsTemplate) {
57  			JmsTemplate template = (JmsTemplate) jmsTemplate;
58  			Assert.isTrue(template.getReceiveTimeout() != JmsTemplate.RECEIVE_TIMEOUT_INDEFINITE_WAIT,
59  					"JmsTemplate must have a receive timeout!");
60  			Assert.isTrue(template.getDefaultDestination() != null || template.getDefaultDestinationName() != null,
61  					"JmsTemplate must have a defaultDestination or defaultDestinationName!");
62  		}
63  	}
64  
65  	/**
66  	 * Set the expected type of incoming message payloads. Set this to
67  	 * {@link Message} to receive the raw underlying message.
68  	 * 
69  	 * @param itemType the java class of the items to be delivered. Typically
70  	 * the same as the class parameter
71  	 * 
72  	 * @throws IllegalStateException if the message payload is of the wrong
73  	 * type.
74  	 */
75  	public void setItemType(Class<? extends T> itemType) {
76  		this.itemType = itemType;
77  	}
78  
79      @Override
80  	@SuppressWarnings("unchecked")
81  	public T read() {
82  		if (itemType != null && itemType.isAssignableFrom(Message.class)) {
83  			return (T) jmsTemplate.receive();
84  		}
85  		Object result = jmsTemplate.receiveAndConvert();
86  		if (itemType != null && result != null) {
87  			Assert.state(itemType.isAssignableFrom(result.getClass()),
88  					"Received message payload of wrong type: expected [" + itemType + "]");
89  		}
90  		return (T) result;
91  	}
92  
93      @Override
94  	public void afterPropertiesSet() throws Exception {
95  		Assert.notNull(this.jmsTemplate, "The 'jmsTemplate' is required.");
96  	}
97  }