polishing
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -24,65 +24,64 @@ import java.util.Map;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.beans.DirectFieldAccessor;
|
||||
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
|
||||
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
|
||||
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
|
||||
|
||||
/**
|
||||
* A default implementation of {@link SqlParamterSourceFactory} which creates an
|
||||
* {@link SqlParameterSource} according to the result of the polled data passed
|
||||
* in
|
||||
* A default implementation of {@link SqlParameterSourceFactory} which creates an
|
||||
* {@link SqlParameterSource} according to the result of the polled data passed in.
|
||||
*
|
||||
* Where the result of the poll is a List a list of ids generated by looking for
|
||||
* a map entry or bean property named by default 'id'. The resulting {@link SqlParameterSource}
|
||||
* contains this list under a default key of 'idList'.
|
||||
* Where the result of the poll is a List, a list of ids is generated by looking for
|
||||
* a map entry or bean property named by default 'id'. The resulting
|
||||
* {@link SqlParameterSource} contains this list under a default key of 'idList'.
|
||||
*
|
||||
* Where the result of the poll is a {@link Map} this is wrapped in an instance of {@link MapSqlParameterSource}
|
||||
* Otherwise the result is wrapped in an instance of {@link BeanPropertySqlParameterSource}
|
||||
* Where the result of the poll is a {@link Map}, this is wrapped in an instance of
|
||||
* {@link MapSqlParameterSource}. Otherwise the result is wrapped in an instance of
|
||||
* {@link BeanPropertySqlParameterSource}.
|
||||
*
|
||||
* @author Jonas Partner
|
||||
*
|
||||
* @since 2.0
|
||||
*/
|
||||
public class DefaultSqlParamterSourceFactory implements
|
||||
SqlParamterSourceFactory {
|
||||
public class DefaultSqlParameterSourceFactory implements SqlParameterSourceFactory {
|
||||
|
||||
private final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
private final Map<String, Object> staticParameters;
|
||||
|
||||
private volatile String polledRowIdName = "id";
|
||||
private final String polledRowIdName = "id";
|
||||
|
||||
private volatile String updateIdsParamName = "idList";
|
||||
private final String updateIdsParamName = "idList";
|
||||
|
||||
public DefaultSqlParamterSourceFactory() {
|
||||
this.staticParameters = Collections
|
||||
.unmodifiableMap(new HashMap<String, Object>());
|
||||
|
||||
public DefaultSqlParameterSourceFactory() {
|
||||
this.staticParameters = Collections.unmodifiableMap(new HashMap<String, Object>());
|
||||
}
|
||||
|
||||
public DefaultSqlParamterSourceFactory(Map<String, Object> staticParameters) {
|
||||
public DefaultSqlParameterSourceFactory(Map<String, Object> staticParameters) {
|
||||
this.staticParameters = Collections.unmodifiableMap(staticParameters);
|
||||
}
|
||||
|
||||
public SqlParameterSource createParamterSource(Object resultOfSelect) {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public SqlParameterSource createParameterSource(Object resultOfSelect) {
|
||||
SqlParameterSource toReturn;
|
||||
if (resultOfSelect instanceof List) {
|
||||
List<Object> ids = new ArrayList<Object>();
|
||||
for (Object rowObj : (List) resultOfSelect) {
|
||||
if (rowObj instanceof Map) {
|
||||
ids.add(((Map) rowObj).get(this.polledRowIdName));
|
||||
} else {
|
||||
DirectFieldAccessor accessor = new DirectFieldAccessor(
|
||||
rowObj);
|
||||
}
|
||||
else {
|
||||
DirectFieldAccessor accessor = new DirectFieldAccessor(rowObj);
|
||||
if (accessor.isReadableProperty(this.polledRowIdName)) {
|
||||
ids
|
||||
.add(accessor
|
||||
.getPropertyValue(this.polledRowIdName));
|
||||
} else {
|
||||
logger
|
||||
.warn("No id field named "
|
||||
+ this.polledRowIdName
|
||||
+ " found for result of polled row. Update may not include all rows");
|
||||
ids.add(accessor.getPropertyValue(this.polledRowIdName));
|
||||
}
|
||||
else {
|
||||
logger.warn("No id field named '" + this.polledRowIdName
|
||||
+ "' found for result of polled row. Update may not include all rows.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -93,14 +92,14 @@ public class DefaultSqlParamterSourceFactory implements
|
||||
thisParamSource.addValue(this.updateIdsParamName, ids);
|
||||
thisParamSource.getValue("idList");
|
||||
toReturn = thisParamSource;
|
||||
} else if (resultOfSelect instanceof Map) {
|
||||
MapSqlParameterSource mapParameterSource = new MapSqlParameterSource(
|
||||
(Map) resultOfSelect);
|
||||
}
|
||||
else if (resultOfSelect instanceof Map) {
|
||||
MapSqlParameterSource mapParameterSource = new MapSqlParameterSource((Map) resultOfSelect);
|
||||
mapParameterSource.addValues(this.staticParameters);
|
||||
toReturn = mapParameterSource;
|
||||
} else {
|
||||
BeanPropertySqlParameterSource beanParameterSource = new BeanPropertySqlParameterSource(
|
||||
resultOfSelect);
|
||||
}
|
||||
else {
|
||||
BeanPropertySqlParameterSource beanParameterSource = new BeanPropertySqlParameterSource(resultOfSelect);
|
||||
toReturn = beanParameterSource;
|
||||
}
|
||||
return toReturn;
|
||||
@@ -1,3 +1,19 @@
|
||||
/*
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
*
|
||||
* Licensed 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.springframework.integration.jdbc;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
@@ -13,6 +29,7 @@ import javax.sql.DataSource;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.integration.core.Message;
|
||||
import org.springframework.integration.jdbc.util.SerializationUtils;
|
||||
import org.springframework.integration.message.MessageBuilder;
|
||||
@@ -34,7 +51,7 @@ import org.springframework.util.StringUtils;
|
||||
* <code>*</code> is the target database type.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*
|
||||
* @since 2.0
|
||||
*/
|
||||
public class JdbcMessageStore implements MessageStore {
|
||||
|
||||
@@ -76,6 +93,7 @@ public class JdbcMessageStore implements MessageStore {
|
||||
|
||||
private MessageMapper mapper = new MessageMapper();
|
||||
|
||||
|
||||
/**
|
||||
* Convenient constructor for configuration use.
|
||||
*/
|
||||
@@ -91,6 +109,7 @@ public class JdbcMessageStore implements MessageStore {
|
||||
jdbcTemplate = new JdbcTemplate(dataSource);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Replace patterns in the input to produce a valid SQL query. This
|
||||
* implementation replaces the table prefix.
|
||||
@@ -126,8 +145,8 @@ public class JdbcMessageStore implements MessageStore {
|
||||
|
||||
/**
|
||||
* The {@link JdbcOperations} to use when interacting with the database.
|
||||
* Either this property can be set or the {@link #setDataSource(DataSource)
|
||||
* dataSource}.
|
||||
* Either this property can be set or the
|
||||
* {@link #setDataSource(DataSource) dataSource}.
|
||||
*
|
||||
* @param dataSource a {@link DataSource}
|
||||
*/
|
||||
@@ -156,21 +175,16 @@ public class JdbcMessageStore implements MessageStore {
|
||||
}
|
||||
|
||||
public Message<?> delete(UUID id) {
|
||||
|
||||
Message<?> message = get(id);
|
||||
if (message == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
int updated = jdbcTemplate.update(getQuery(DELETE_MESSAGE), new Object[] { getKey(id) },
|
||||
new int[] { Types.VARCHAR });
|
||||
|
||||
if (updated != 0) {
|
||||
return message;
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
public Message<?> get(UUID id) {
|
||||
@@ -182,12 +196,11 @@ public class JdbcMessageStore implements MessageStore {
|
||||
}
|
||||
|
||||
public List<Message<?>> list(Object correlationId) {
|
||||
return jdbcTemplate.query(getQuery(LIST_MESSAGES_BY_CORRELATION_KEY), new Object[] { getKey(correlationId) },
|
||||
mapper);
|
||||
return jdbcTemplate.query(getQuery(LIST_MESSAGES_BY_CORRELATION_KEY),
|
||||
new Object[] { getKey(correlationId) }, mapper);
|
||||
}
|
||||
|
||||
public <T> Message<T> put(final Message<T> message) {
|
||||
|
||||
if (message.getHeaders().containsKey(SAVED_KEY)) {
|
||||
@SuppressWarnings("unchecked")
|
||||
Message<T> saved = (Message<T>) get(message.getHeaders().getId());
|
||||
@@ -214,41 +227,15 @@ public class JdbcMessageStore implements MessageStore {
|
||||
lobHandler.getLobCreator().setBlobAsBytes(ps, 4, messageBytes);
|
||||
}
|
||||
});
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
private String getKey(Object input) {
|
||||
return input==null ? null : UUIDConverter.getUUID(input).toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience class to be used to unpack a message from a result set row.
|
||||
* Uses column named in the result set to extract the required data, so that
|
||||
* select clause ordering is unimportant.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
private class MessageMapper implements RowMapper<Message<?>> {
|
||||
|
||||
public Message<?> mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
Message<?> message = (Message<?>) SerializationUtils.deserialize(lobHandler.getBlobAsBytes(rs,
|
||||
"MESSAGE_BYTES"));
|
||||
return message;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void put(Object correlationId, Message<?> message) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
public void put(Object correlationId, Collection<Message<?>> messages) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
public Message<?> delete(Object correlationId, UUID messageId) {
|
||||
@@ -258,7 +245,27 @@ public class JdbcMessageStore implements MessageStore {
|
||||
|
||||
public void deleteAll(Object correlationId) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
private String getKey(Object input) {
|
||||
return input == null ? null : UUIDConverter.getUUID(input).toString();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Convenience class to be used to unpack a message from a result set row.
|
||||
* Uses column named in the result set to extract the required data, so that
|
||||
* select clause ordering is unimportant.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*/
|
||||
private class MessageMapper implements RowMapper<Message<?>> {
|
||||
|
||||
public Message<?> mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
Message<?> message = (Message<?>) SerializationUtils.deserialize(
|
||||
lobHandler.getBlobAsBytes(rs, "MESSAGE_BYTES"));
|
||||
return message;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -30,10 +30,11 @@ import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
|
||||
|
||||
/**
|
||||
* A polling channel adapter that creates messages from the payload returned by
|
||||
* executing a select query Optionally an update can be executed after the
|
||||
* select in order to update processed rows
|
||||
* executing a select query. Optionally an update can be executed after the
|
||||
* select in order to update processed rows.
|
||||
*
|
||||
* @author Jonas Partner
|
||||
* @since 2.0
|
||||
*/
|
||||
public class JdbcPollingChannelAdapter implements MessageSource<Object> {
|
||||
|
||||
@@ -49,16 +50,15 @@ public class JdbcPollingChannelAdapter implements MessageSource<Object> {
|
||||
|
||||
private volatile String updateSql;
|
||||
|
||||
private volatile SqlParamterSourceFactory sqlParameterSourceFactoryForUpdate = new DefaultSqlParamterSourceFactory();
|
||||
private volatile SqlParameterSourceFactory sqlParameterSourceFactoryForUpdate = new DefaultSqlParameterSourceFactory();
|
||||
|
||||
|
||||
/**
|
||||
* Constructor taking query to execute to retreive new rows and
|
||||
* {@link DataSource} from which the DB Connection can be obtained
|
||||
* Constructor taking {@link DataSource} from which the DB Connection can
|
||||
* be obtained and the select query to execute to retrieve new rows.
|
||||
*
|
||||
* @param dataSource
|
||||
* used to create a {@link SimpleJdbcTemplate}
|
||||
* @param selectQuery
|
||||
* query to execute
|
||||
* @param dataSource used to create a {@link SimpleJdbcTemplate}
|
||||
* @param selectQuery query to execute
|
||||
*/
|
||||
public JdbcPollingChannelAdapter(DataSource dataSource, String selectQuery) {
|
||||
this.jdbcOperations = new SimpleJdbcTemplate(dataSource);
|
||||
@@ -66,19 +66,18 @@ public class JdbcPollingChannelAdapter implements MessageSource<Object> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor taking query to execute on a poll and
|
||||
* {@link SimpleJdbcOperations} instance to use for query execution
|
||||
* Constructor taking {@link SimpleJdbcOperations} instance to use for query
|
||||
* execution and the select query to execute to retrieve new rows.
|
||||
*
|
||||
* @param jdbcOperations
|
||||
* @param selectQuery
|
||||
* query to execute
|
||||
* @param jdbcOperations instance to use for query execution
|
||||
* @param selectQuery query to execute
|
||||
*/
|
||||
public JdbcPollingChannelAdapter(SimpleJdbcOperations jdbcOperations,
|
||||
String selectQuery) {
|
||||
public JdbcPollingChannelAdapter(SimpleJdbcOperations jdbcOperations, String selectQuery) {
|
||||
this.jdbcOperations = jdbcOperations;
|
||||
this.selectQuery = selectQuery;
|
||||
}
|
||||
|
||||
|
||||
public void setRowMapper(RowMapper<?> rowMapper) {
|
||||
this.rowMapper = rowMapper;
|
||||
}
|
||||
@@ -91,18 +90,18 @@ public class JdbcPollingChannelAdapter implements MessageSource<Object> {
|
||||
this.updatePerRow = updatePerRow;
|
||||
}
|
||||
|
||||
public void setSqlParameterSourceFactoryForUpdate(
|
||||
SqlParamterSourceFactory sqlParameterSourceFactoryForUpdate) {
|
||||
public void setSqlParameterSourceFactoryForUpdate(SqlParameterSourceFactory sqlParameterSourceFactoryForUpdate) {
|
||||
this.sqlParameterSourceFactoryForUpdate = sqlParameterSourceFactoryForUpdate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Polls for new rows returning a message containing one or more rows where
|
||||
* rows are found and null where no rows are returned by the select query
|
||||
* Executes the query. If a query result set contains one or more rows, the Message
|
||||
* payload will contain either a List of Maps for each row or, if a RowMapper has
|
||||
* been provided, the values mapped from those rows. If the query returns no rows,
|
||||
* this method will return <code>null</code>.
|
||||
*/
|
||||
public Message<Object> receive() {
|
||||
Object payload = null;
|
||||
payload = pollAndUpdate();
|
||||
Object payload = pollAndUpdate();
|
||||
if (payload == null) {
|
||||
return null;
|
||||
}
|
||||
@@ -110,59 +109,54 @@ public class JdbcPollingChannelAdapter implements MessageSource<Object> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the select query and the update query if provided and rows are
|
||||
* returned by the select query
|
||||
*
|
||||
* @return
|
||||
* Execute the select query and the update query if provided.
|
||||
* Returns the rows returned by the select query. If a RowMapper
|
||||
* has been provided, the mapped results are returned.
|
||||
*/
|
||||
protected Object pollAndUpdate() {
|
||||
List payload;
|
||||
private Object pollAndUpdate() {
|
||||
List<?> payload;
|
||||
if (this.rowMapper != null) {
|
||||
payload = pollWithRowMapper();
|
||||
} else {
|
||||
payload = this.jdbcOperations.queryForList(this.selectQuery,
|
||||
this.sqlQueryParameterSource);
|
||||
}
|
||||
|
||||
else {
|
||||
payload = this.jdbcOperations.queryForList(this.selectQuery, this.sqlQueryParameterSource);
|
||||
}
|
||||
if (payload.size() < 1) {
|
||||
payload = null;
|
||||
}
|
||||
|
||||
if (payload != null && updateSql != null) {
|
||||
if (this.updatePerRow) {
|
||||
for (Object row : payload) {
|
||||
executeUpdateQuery(row);
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
executeUpdateQuery(payload);
|
||||
}
|
||||
}
|
||||
return payload;
|
||||
|
||||
}
|
||||
|
||||
protected void executeUpdateQuery(Object obj) {
|
||||
private void executeUpdateQuery(Object obj) {
|
||||
SqlParameterSource updateParamaterSource = null;
|
||||
if (this.sqlParameterSourceFactoryForUpdate != null) {
|
||||
|
||||
updateParamaterSource = this.sqlParameterSourceFactoryForUpdate
|
||||
.createParamterSource(obj);
|
||||
updateParamaterSource = this.sqlParameterSourceFactoryForUpdate.createParameterSource(obj);
|
||||
this.jdbcOperations.update(this.updateSql, updateParamaterSource);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
this.jdbcOperations.update(this.updateSql);
|
||||
}
|
||||
}
|
||||
|
||||
protected List pollWithRowMapper() {
|
||||
List payload = null;
|
||||
private List<?> pollWithRowMapper() {
|
||||
List<?> payload = null;
|
||||
if (this.sqlQueryParameterSource != null) {
|
||||
payload = this.jdbcOperations.query(this.selectQuery,
|
||||
this.rowMapper, this.sqlQueryParameterSource);
|
||||
} else {
|
||||
payload = this.jdbcOperations.query(this.selectQuery,
|
||||
this.rowMapper);
|
||||
payload = this.jdbcOperations.query(this.selectQuery, this.rowMapper, this.sqlQueryParameterSource);
|
||||
}
|
||||
else {
|
||||
payload = this.jdbcOperations.query(this.selectQuery, this.rowMapper);
|
||||
}
|
||||
return payload;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -21,18 +21,17 @@ import org.springframework.jdbc.core.namedparam.SqlParameterSource;
|
||||
/**
|
||||
* Collaborator for {@link JdbcPollingChannelAdapter} which allows creation of
|
||||
* instances of {@link SqlParameterSource} for use in updates to be created
|
||||
* according to the result of the poll
|
||||
* according to the result of the poll.
|
||||
*
|
||||
* @author Jonas Partner
|
||||
*
|
||||
* @since 2.0
|
||||
*/
|
||||
public interface SqlParamterSourceFactory {
|
||||
public interface SqlParameterSourceFactory {
|
||||
|
||||
/**
|
||||
* Return a new {@link SqlParameterSource}
|
||||
* @param obj the result of the preceeding poll operation
|
||||
* @return
|
||||
* Return a new {@link SqlParameterSource}.
|
||||
* @param pollResult the result of the preceding poll operation
|
||||
*/
|
||||
public SqlParameterSource createParamterSource(Object obj);
|
||||
public SqlParameterSource createParameterSource(Object resultOfSelect);
|
||||
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -13,14 +13,16 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.jdbc.config;
|
||||
|
||||
import org.springframework.integration.config.xml.AbstractIntegrationNamespaceHandler;
|
||||
|
||||
/**
|
||||
* Namespace handler for the integration JDBC schema.
|
||||
*
|
||||
* @author Jonas Partner
|
||||
*
|
||||
* @since 2.0
|
||||
*/
|
||||
public class JdbcNamespaceHandler extends AbstractIntegrationNamespaceHandler {
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.springframework.integration.jdbc.config;
|
||||
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
|
||||
@@ -23,15 +25,14 @@ import org.springframework.beans.factory.xml.ParserContext;
|
||||
import org.springframework.integration.config.xml.AbstractPollingInboundChannelAdapterParser;
|
||||
import org.springframework.integration.config.xml.IntegrationNamespaceUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Parser for {@link JdbcPollingChannelAdapterParser}
|
||||
* Parser for {@link org.springframework.integration.jdbc.JdbcPollingChannelAdapter}.
|
||||
*
|
||||
* @author Jonas Partner
|
||||
*
|
||||
* @since 2.0
|
||||
*/
|
||||
public class JdbcPollingChannelAdapterParser extends
|
||||
AbstractPollingInboundChannelAdapterParser {
|
||||
public class JdbcPollingChannelAdapterParser extends AbstractPollingInboundChannelAdapterParser {
|
||||
|
||||
protected boolean shouldGenerateId() {
|
||||
return false;
|
||||
@@ -40,44 +41,37 @@ public class JdbcPollingChannelAdapterParser extends
|
||||
protected boolean shouldGenerateIdAsFallback() {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
protected String parseSource(Element element, ParserContext parserContext) {
|
||||
Object source = parserContext.extractSource(element);
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder
|
||||
.genericBeanDefinition("org.springframework.integration.jdbc.JdbcPollingChannelAdapter");
|
||||
String dataSourceRef = element.getAttribute("data-source");
|
||||
String simpleJdbcOperationsRef = element
|
||||
.getAttribute("simple-jdbc-operations");
|
||||
String simpleJdbcOperationsRef = element.getAttribute("simple-jdbc-operations");
|
||||
boolean refToDataSourceSet = StringUtils.hasText(dataSourceRef);
|
||||
boolean refToSimpleJdbcOperaitonsSet = StringUtils
|
||||
.hasText(simpleJdbcOperationsRef);
|
||||
|
||||
boolean refToSimpleJdbcOperaitonsSet = StringUtils.hasText(simpleJdbcOperationsRef);
|
||||
if ((refToDataSourceSet && refToSimpleJdbcOperaitonsSet)
|
||||
|| (!refToDataSourceSet && !refToSimpleJdbcOperaitonsSet)) {
|
||||
throw new BeanCreationException(
|
||||
"Exactly one of the attributes data-source or simple-jdbc-operations should be set for the JDBC inbound-channel-adapter");
|
||||
parserContext.getReaderContext().error("Exactly one of the attributes data-source or " +
|
||||
"simple-jdbc-operations should be set for the JDBC inbound-channel-adapter", source);
|
||||
}
|
||||
|
||||
String query = element.getAttribute("query");
|
||||
if(!StringUtils.hasText(query)){
|
||||
if (!StringUtils.hasText(query)) {
|
||||
throw new BeanCreationException("The query attrbitue is required");
|
||||
}
|
||||
if(refToDataSourceSet){
|
||||
if (refToDataSourceSet) {
|
||||
builder.addConstructorArgReference(dataSourceRef);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
builder.addConstructorArgReference(simpleJdbcOperationsRef);
|
||||
}
|
||||
builder.addConstructorArgValue(query);
|
||||
|
||||
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "row-mapper");
|
||||
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "update", "updateSql");
|
||||
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "update-per-row");
|
||||
|
||||
|
||||
|
||||
return BeanDefinitionReaderUtils.registerWithGeneratedName(builder
|
||||
.getBeanDefinition(), parserContext.getRegistry());
|
||||
return BeanDefinitionReaderUtils.registerWithGeneratedName(
|
||||
builder.getBeanDefinition(), parserContext.getRegistry());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.jdbc.util;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
@@ -21,12 +22,10 @@ import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
|
||||
|
||||
/**
|
||||
* Static utility to help with serialization.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class SerializationUtils {
|
||||
|
||||
@@ -37,20 +36,17 @@ public class SerializationUtils {
|
||||
* @return an array of bytes representing the object in a portable fashion
|
||||
*/
|
||||
public static byte[] serialize(Object object) {
|
||||
|
||||
if (object==null) {
|
||||
if (object == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
try {
|
||||
new ObjectOutputStream(stream).writeObject(object);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalArgumentException("Could not serialize object of type: "+object.getClass(), e);
|
||||
}
|
||||
|
||||
catch (IOException e) {
|
||||
throw new IllegalArgumentException("Could not serialize object of type: " + object.getClass(), e);
|
||||
}
|
||||
return stream.toByteArray();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -58,11 +54,9 @@ public class SerializationUtils {
|
||||
* @return the result of deserializing the bytes
|
||||
*/
|
||||
public static Object deserialize(byte[] bytes) {
|
||||
|
||||
if (bytes==null) {
|
||||
if (bytes == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
return new ObjectInputStream(new ByteArrayInputStream(bytes)).readObject();
|
||||
}
|
||||
@@ -72,7 +66,6 @@ public class SerializationUtils {
|
||||
catch (ClassNotFoundException e) {
|
||||
throw new IllegalStateException("Could not deserialize object type", e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<xsd:schema xmlns="http://www.springframework.org/schema/integration/jdbc"
|
||||
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:beans="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
|
||||
xmlns:beans="http://www.springframework.org/schema/beans"
|
||||
xmlns:tool="http://www.springframework.org/schema/tool"
|
||||
xmlns:integration="http://www.springframework.org/schema/integration"
|
||||
targetNamespace="http://www.springframework.org/schema/integration/jdbc"
|
||||
@@ -17,24 +18,22 @@
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
|
||||
|
||||
<xsd:element name="inbound-channel-adapter">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
Defines an inbound Channel Adapter for polling a database.
|
||||
Defines an inbound Channel Adapter for polling a database.
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element ref="integration:poller" minOccurs="0"
|
||||
maxOccurs="1" />
|
||||
<xsd:element ref="integration:poller" minOccurs="0" maxOccurs="1" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="simple-jdbc-operations" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:appinfo>
|
||||
<tool:annotation kind="ref">
|
||||
<tool:expected-type
|
||||
type="import org.springframework.jdbc.core.simple.SimpleJdbcOperations" />
|
||||
type="org.springframework.jdbc.core.simple.SimpleJdbcOperations" />
|
||||
</tool:annotation>
|
||||
</xsd:appinfo>
|
||||
</xsd:annotation>
|
||||
@@ -43,7 +42,7 @@
|
||||
<xsd:annotation>
|
||||
<xsd:appinfo>
|
||||
<tool:annotation kind="ref">
|
||||
<tool:expected-type type="import javax.sql.DataSource" />
|
||||
<tool:expected-type type="javax.sql.DataSource" />
|
||||
</tool:annotation>
|
||||
</xsd:appinfo>
|
||||
</xsd:annotation>
|
||||
@@ -54,14 +53,13 @@
|
||||
<xsd:appinfo>
|
||||
<tool:annotation kind="ref">
|
||||
<tool:expected-type
|
||||
type="import org.springframework.jdbc.core.RowMapper" />
|
||||
type="org.springframework.jdbc.core.RowMapper" />
|
||||
</tool:annotation>
|
||||
</xsd:appinfo>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="update" type="xsd:string" />
|
||||
<xsd:attribute name="update-per-row" type="xsd:boolean"
|
||||
default="false" />
|
||||
<xsd:attribute name="update-per-row" type="xsd:boolean" default="false" />
|
||||
<xsd:attribute name="channel" type="xsd:string" use="required">
|
||||
<xsd:annotation>
|
||||
<xsd:appinfo>
|
||||
@@ -74,4 +72,5 @@
|
||||
</xsd:attribute>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
</xsd:schema>
|
||||
@@ -1,24 +1,22 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
|
||||
<jdbc:embedded-database id="dataSource" type="DERBY">
|
||||
<jdbc:script location="${int.schema.script}" />
|
||||
</jdbc:embedded-database>
|
||||
|
||||
<bean id="placeholderProperties"
|
||||
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
|
||||
<property name="location"
|
||||
value="classpath:int-${ENVIRONMENT:derby}.properties" />
|
||||
<bean id="placeholderProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
|
||||
<property name="location" value="classpath:int-${ENVIRONMENT:derby}.properties" />
|
||||
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
|
||||
<property name="ignoreUnresolvablePlaceholders" value="true" />
|
||||
<property name="order" value="1" />
|
||||
</bean>
|
||||
|
||||
<bean id="transactionManager"
|
||||
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
|
||||
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
|
||||
<property name="dataSource" ref="dataSource" />
|
||||
</bean>
|
||||
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans:beans xmlns="http://www.springframework.org/schema/integration/jdbc"
|
||||
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:si="http://www.springframework.org/schema/integration"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
|
||||
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
xmlns:beans="http://www.springframework.org/schema/beans"
|
||||
xmlns:si="http://www.springframework.org/schema/integration"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
|
||||
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
|
||||
<si:channel id="target">
|
||||
<si:queue />
|
||||
@@ -14,13 +16,12 @@
|
||||
<jdbc:script location="org/springframework/integration/jdbc/config/inboundSchema.sql" />
|
||||
</jdbc:embedded-database>
|
||||
|
||||
<beans:bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
|
||||
<beans:bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
|
||||
<beans:property name="dataSource" ref="dataSource"/>
|
||||
</beans:bean>
|
||||
|
||||
<si:poller default="true">
|
||||
<si:interval-trigger interval="1" />
|
||||
<si:interval-trigger interval="100" />
|
||||
</si:poller>
|
||||
|
||||
|
||||
</beans:beans>
|
||||
@@ -11,7 +11,7 @@
|
||||
http://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc.xsd">
|
||||
|
||||
<inbound-channel-adapter query="select * from item where status=2" channel="target"
|
||||
data-source="dataSource" />
|
||||
data-source="dataSource" />
|
||||
|
||||
<beans:import resource="jdbcInboundChannelAdapterCommonConfig.xml" />
|
||||
|
||||
|
||||
@@ -3,20 +3,16 @@
|
||||
xmlns:beans="http://www.springframework.org/schema/beans"
|
||||
xmlns:si="http://www.springframework.org/schema/integration"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/integration
|
||||
http://www.springframework.org/schema/integration/spring-integration.xsd
|
||||
http://www.springframework.org/schema/integration/jdbc
|
||||
http://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc.xsd">
|
||||
|
||||
|
||||
|
||||
|
||||
<inbound-channel-adapter query="select * from item where status=2" channel="target"
|
||||
data-source="dataSource" update="update item set status =10 where id in (:idList)" />
|
||||
|
||||
data-source="dataSource" update="update item set status =10 where id in (:idList)" />
|
||||
|
||||
<beans:import resource="jdbcInboundChannelAdapterCommonConfig.xml" />
|
||||
|
||||
|
||||
</beans:beans>
|
||||
|
||||
Reference in New Issue
Block a user