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.RuntimeCamelException;
031 import org.apache.camel.component.jt400.Jt400DataQueueEndpoint.Format;
032 import org.apache.camel.impl.DefaultExchange;
033 import org.apache.camel.impl.PollingConsumerSupport;
034
035
036 /**
037 * {@link PollingConsumer} that polls a data queue for data
038 */
039 public class Jt400DataQueueConsumer extends PollingConsumerSupport {
040
041 private final Jt400DataQueueEndpoint endpoint;
042
043 /**
044 * Creates a new consumer instance
045 */
046 protected Jt400DataQueueConsumer(Jt400DataQueueEndpoint endpoint) {
047 super(endpoint);
048 this.endpoint = endpoint;
049 }
050
051 @Override
052 protected void doStart() throws Exception {
053 if (!endpoint.getSystem().isConnected()) {
054 endpoint.getSystem().connectService(AS400.DATAQUEUE);
055 }
056 }
057
058 @Override
059 protected void doStop() throws Exception {
060 if (endpoint.getSystem().isConnected()) {
061 endpoint.getSystem().disconnectAllServices();
062 }
063 }
064
065 /**
066 * {@link Jt400DataQueueConsumer#receive(long)}
067 */
068 public Exchange receive() {
069 // -1 to indicate a blocking read from data queue
070 return receive(-1);
071 }
072
073 /**
074 * {@link Jt400DataQueueConsumer#receive(long)}
075 */
076 public Exchange receiveNoWait() {
077 return receive(0);
078 }
079
080 /**
081 * Receives an entry from a data queue and returns an {@link Exchange} to
082 * send this data If the endpoint's format is set to {@link Format#binary},
083 * the data queue entry's data will be received/sent as a
084 * <code>byte[]</code>. If the endpoint's format is set to
085 * {@link Format#text}, the data queue entry's data will be received/sent as
086 * a <code>String</code>.
087 *
088 * @param timeout time to wait when reading from data queue. A value of -1
089 * indicates a blocking read.
090 */
091 public Exchange receive(long timeout) {
092 DataQueue queue = endpoint.getDataQueue();
093 try {
094 DataQueueEntry entry;
095 if (timeout >= 0) {
096 entry = queue.read((int)timeout);
097 } else {
098 entry = queue.read();
099 }
100 Exchange exchange = new DefaultExchange(endpoint.getCamelContext());
101 if (entry != null) {
102 if (endpoint.getFormat() == Format.binary) {
103 exchange.getIn().setBody(entry.getData());
104 } else {
105 exchange.getIn().setBody(entry.getString());
106 }
107 return exchange;
108 }
109 } catch (AS400SecurityException e) {
110 throw new RuntimeCamelException("Unable to read from data queue: " + e.getMessage(), e);
111 } catch (ErrorCompletingRequestException e) {
112 throw new RuntimeCamelException("Unable to read from data queue: " + e.getMessage(), e);
113 } catch (IOException e) {
114 throw new RuntimeCamelException("Unable to read from data queue: " + e.getMessage(), e);
115 } catch (IllegalObjectTypeException e) {
116 throw new RuntimeCamelException("Unable to read from data queue: " + e.getMessage(), e);
117 } catch (InterruptedException e) {
118 throw new RuntimeCamelException("Unable to read from data queue: " + e.getMessage(), e);
119 } catch (ObjectDoesNotExistException e) {
120 throw new RuntimeCamelException("Unable to read from data queue: " + e.getMessage(), e);
121 }
122 return null;
123 }
124 }