001/*
002 * Copyright 2017 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.builder;
018
019import javax.jms.Message;
020
021import org.springframework.batch.item.jms.JmsItemReader;
022import org.springframework.jms.core.JmsOperations;
023import org.springframework.util.Assert;
024
025/**
026 * Creates a fully qualified JmsItemReader.
027 *
028 * @author Glenn Renfro
029 *
030 * @since 4.0
031 */
032public class JmsItemReaderBuilder<T> {
033
034        protected Class<? extends T> itemType;
035
036        protected JmsOperations jmsTemplate;
037
038        /**
039         * Establish the JMS template that will be used by the JmsItemReader.
040         *
041         * @param jmsTemplate a {@link JmsOperations} instance
042         * @return this instance for method chaining.
043         * @see JmsItemReader#setJmsTemplate(JmsOperations)
044         */
045        public JmsItemReaderBuilder<T> jmsTemplate(JmsOperations jmsTemplate) {
046                this.jmsTemplate = jmsTemplate;
047
048                return this;
049        }
050
051        /**
052         * Set the expected type of incoming message payloads. Set this to {@link Message} to
053         * receive the raw underlying message.
054         *
055         * @param itemType the java class of the items to be delivered. Typically the same as
056         * the class parameter
057         * @return this instance for method chaining.
058         *
059         * @throws IllegalStateException if the message payload is of the wrong type.
060         * @see JmsItemReader#setItemType(Class)
061         */
062        public JmsItemReaderBuilder<T> itemType(Class<? extends T> itemType) {
063                this.itemType = itemType;
064
065                return this;
066        }
067
068        /**
069         * Returns a fully constructed {@link JmsItemReader}.
070         *
071         * @return a new {@link JmsItemReader}
072         */
073        public JmsItemReader<T> build() {
074                Assert.notNull(this.jmsTemplate, "jmsTemplate is required.");
075                JmsItemReader<T> jmsItemReader = new JmsItemReader<>();
076
077                jmsItemReader.setItemType(this.itemType);
078                jmsItemReader.setJmsTemplate(this.jmsTemplate);
079                return jmsItemReader;
080        }
081}