001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.component.jt400;
018    
019    import java.io.IOException;
020    
021    import com.ibm.as400.access.AS400;
022    import com.ibm.as400.access.AS400SecurityException;
023    import com.ibm.as400.access.DataQueue;
024    import com.ibm.as400.access.DataQueueEntry;
025    import com.ibm.as400.access.ErrorCompletingRequestException;
026    import com.ibm.as400.access.IllegalObjectTypeException;
027    import com.ibm.as400.access.ObjectDoesNotExistException;
028    
029    import org.apache.camel.Exchange;
030    import org.apache.camel.PollingConsumer;
031    import org.apache.camel.RuntimeCamelException;
032    import org.apache.camel.component.jt400.Jt400DataQueueEndpoint.Format;
033    import org.apache.camel.impl.DefaultExchange;
034    import org.apache.camel.impl.PollingConsumerSupport;
035    
036    
037    /**
038     * {@link PollingConsumer} that polls a data queue for data
039     */
040    public class Jt400DataQueueConsumer extends PollingConsumerSupport {
041    
042        private final Jt400DataQueueEndpoint endpoint;
043    
044        /**
045         * Creates a new consumer instance
046         */
047        protected Jt400DataQueueConsumer(Jt400DataQueueEndpoint endpoint) {
048            super(endpoint);
049            this.endpoint = endpoint;
050        }
051    
052        @Override
053        protected void doStart() throws Exception {
054            if (!endpoint.getSystem().isConnected()) {
055                endpoint.getSystem().connectService(AS400.DATAQUEUE);
056            }
057        }
058    
059        @Override
060        protected void doStop() throws Exception {
061            if (endpoint.getSystem().isConnected()) {
062                endpoint.getSystem().disconnectAllServices();
063            }
064        }
065    
066        /**
067         * {@link Jt400DataQueueConsumer#receive(long)}
068         */
069        public Exchange receive() {
070            // -1 to indicate a blocking read from data queue
071            return receive(-1);
072        }
073    
074        /**
075         * {@link Jt400DataQueueConsumer#receive(long)}
076         */
077        public Exchange receiveNoWait() {
078            return receive(0);
079        }
080    
081        /**
082         * Receives an entry from a data queue and returns an {@link Exchange} to
083         * send this data If the endpoint's format is set to {@link Format#binary},
084         * the data queue entry's data will be received/sent as a
085         * <code>byte[]</code>. If the endpoint's format is set to
086         * {@link Format#text}, the data queue entry's data will be received/sent as
087         * a <code>String</code>.
088         *
089         * @param timeout time to wait when reading from data queue. A value of -1
090         *            indicates a blocking read.
091         */
092        public Exchange receive(long timeout) {
093            DataQueue queue = endpoint.getDataQueue();
094            try {
095                DataQueueEntry entry;
096                if (timeout >= 0) {
097                    entry = queue.read((int)timeout);
098                } else {
099                    entry = queue.read();
100                }
101                Exchange exchange = new DefaultExchange(endpoint.getCamelContext());
102                if (entry != null) {
103                    if (endpoint.getFormat() == Format.binary) {
104                        exchange.getIn().setBody(entry.getData());
105                    } else {
106                        exchange.getIn().setBody(entry.getString());
107                    }
108                    return exchange;
109                }
110            } catch (AS400SecurityException e) {
111                throw new RuntimeCamelException("Unable to read from data queue: " + e.getMessage(), e);
112            } catch (ErrorCompletingRequestException e) {
113                throw new RuntimeCamelException("Unable to read from data queue: " + e.getMessage(), e);
114            } catch (IOException e) {
115                throw new RuntimeCamelException("Unable to read from data queue: " + e.getMessage(), e);
116            } catch (IllegalObjectTypeException e) {
117                throw new RuntimeCamelException("Unable to read from data queue: " + e.getMessage(), e);
118            } catch (InterruptedException e) {
119                throw new RuntimeCamelException("Unable to read from data queue: " + e.getMessage(), e);
120            } catch (ObjectDoesNotExistException e) {
121                throw new RuntimeCamelException("Unable to read from data queue: " + e.getMessage(), e);
122            }
123            return null;
124        }
125    }