org.apache.camel.component.mybatis.MyBatisConsumer Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.component.mybatis;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import org.apache.camel.Exchange;
import org.apache.camel.ExchangePattern;
import org.apache.camel.ExchangePropertyKey;
import org.apache.camel.Message;
import org.apache.camel.Processor;
import org.apache.camel.RollbackExchangeException;
import org.apache.camel.ShutdownRunningTask;
import org.apache.camel.support.ScheduledBatchPollingConsumer;
import org.apache.camel.util.CastUtils;
import org.apache.camel.util.ObjectHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Consumer to read data from a database.
*/
public class MyBatisConsumer extends ScheduledBatchPollingConsumer {
static final class DataHolder {
Exchange exchange;
Object data;
DataHolder() {
}
}
private static final Logger LOG = LoggerFactory.getLogger(MyBatisConsumer.class);
protected volatile ShutdownRunningTask shutdownRunningTask;
protected volatile int pendingExchanges;
private String onConsume;
private boolean useIterator = true;
private boolean routeEmptyResultSet;
public MyBatisConsumer(MyBatisEndpoint endpoint, Processor processor) {
super(endpoint, processor);
}
@Override
public MyBatisEndpoint getEndpoint() {
return (MyBatisEndpoint) super.getEndpoint();
}
/**
* Polls the database
*/
@Override
protected int poll() throws Exception {
// must reset for each poll
shutdownRunningTask = null;
pendingExchanges = 0;
// poll data from the database
MyBatisEndpoint endpoint = getEndpoint();
LOG.trace("Polling: {}", endpoint);
List> data = endpoint.getProcessingStrategy().poll(this, getEndpoint());
// okay we have some response from MyBatis so lets mark the consumer as ready
forceConsumerAsReady();
// create a list of exchange objects with the data
Queue answer = new LinkedList<>();
if (useIterator) {
for (Object item : data) {
Exchange exchange = createExchange(item);
DataHolder holder = new DataHolder();
holder.exchange = exchange;
holder.data = item;
answer.add(holder);
}
} else {
if (!data.isEmpty() || routeEmptyResultSet) {
Exchange exchange = createExchange(data);
DataHolder holder = new DataHolder();
holder.exchange = exchange;
holder.data = data;
answer.add(holder);
}
}
// process all the exchanges in this batch
return processBatch(CastUtils.cast(answer));
}
@Override
public int processBatch(Queue
© 2015 - 2025 Weber Informatics LLC | Privacy Policy