Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* 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.sql;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
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.Message;
import org.apache.camel.Processor;
import org.apache.camel.impl.ScheduledBatchPollingConsumer;
import org.apache.camel.spi.UriParam;
import org.apache.camel.util.CastUtils;
import org.apache.camel.util.ObjectHelper;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCallback;
import static org.springframework.jdbc.support.JdbcUtils.closeResultSet;
public class SqlConsumer extends ScheduledBatchPollingConsumer {
private final String query;
private final JdbcTemplate jdbcTemplate;
private final SqlPrepareStatementStrategy sqlPrepareStatementStrategy;
private final SqlProcessingStrategy sqlProcessingStrategy;
@UriParam
private String onConsume;
@UriParam
private String onConsumeFailed;
@UriParam
private String onConsumeBatchComplete;
@UriParam
private boolean useIterator = true;
@UriParam
private boolean routeEmptyResultSet;
@UriParam
private int expectedUpdateCount = -1;
@UriParam
private boolean breakBatchOnConsumeFail;
private static final class DataHolder {
private Exchange exchange;
private Object data;
private DataHolder() {
}
}
public SqlConsumer(SqlEndpoint endpoint, Processor processor, JdbcTemplate jdbcTemplate, String query,
SqlPrepareStatementStrategy sqlPrepareStatementStrategy, SqlProcessingStrategy sqlProcessingStrategy) {
super(endpoint, processor);
this.jdbcTemplate = jdbcTemplate;
this.query = query;
this.sqlPrepareStatementStrategy = sqlPrepareStatementStrategy;
this.sqlProcessingStrategy = sqlProcessingStrategy;
}
@Override
public SqlEndpoint getEndpoint() {
return (SqlEndpoint) super.getEndpoint();
}
@Override
protected int poll() throws Exception {
// must reset for each poll
shutdownRunningTask = null;
pendingExchanges = 0;
final String preparedQuery = sqlPrepareStatementStrategy.prepareQuery(query, getEndpoint().isAllowNamedParameters());
Integer messagePolled = jdbcTemplate.execute(preparedQuery, new PreparedStatementCallback() {
@Override
public Integer doInPreparedStatement(PreparedStatement preparedStatement) throws SQLException, DataAccessException {
Queue answer = new LinkedList();
log.debug("Executing query: {}", preparedQuery);
ResultSet rs = preparedStatement.executeQuery();
SqlOutputType outputType = getEndpoint().getOutputType();
try {
log.trace("Got result list from query: {}, outputType={}", rs, outputType);
if (outputType == SqlOutputType.SelectList) {
List> data = getEndpoint().queryForList(rs, true);
addListToQueue(data, answer);
} else if (outputType == SqlOutputType.SelectOne) {
Object data = getEndpoint().queryForObject(rs);
if (data != null) {
addListToQueue(data, answer);
}
} else {
throw new IllegalArgumentException("Invalid outputType=" + outputType);
}
} finally {
closeResultSet(rs);
}
// process all the exchanges in this batch
try {
int rows = processBatch(CastUtils.cast(answer));
return rows;
} catch (Exception e) {
throw ObjectHelper.wrapRuntimeCamelException(e);
}
}
});
return messagePolled;
}
private void addListToQueue(Object data, Queue answer) {
if (data instanceof List) {
// create a list of exchange objects with the data
List> list = (List)data;
if (useIterator) {
for (Object item : list) {
addItemToQueue(item, answer);
}
} else if (!list.isEmpty() || routeEmptyResultSet) {
addItemToQueue(list, answer);
}
} else {
// create single object as data
addItemToQueue(data, answer);
}
}
private void addItemToQueue(Object item, Queue answer) {
Exchange exchange = createExchange(item);
DataHolder holder = new DataHolder();
holder.exchange = exchange;
holder.data = item;
answer.add(holder);
}
protected Exchange createExchange(Object data) {
final Exchange exchange = getEndpoint().createExchange(ExchangePattern.InOnly);
Message msg = exchange.getIn();
if (getEndpoint().getOutputHeader() != null) {
msg.setHeader(getEndpoint().getOutputHeader(), data);
} else {
msg.setBody(data);
}
return exchange;
}
@Override
public int processBatch(Queue