INTSAMPLE-33 Changes
This commit is contained in:
@@ -20,6 +20,36 @@ Currently one example exists. On the command prompt you can enter the following
|
||||
* 'b'
|
||||
* 'foo'
|
||||
|
||||
#Sample "Person Outbound Gateway"
|
||||
|
||||
We use the outbound gateway to insert records in a Person table based on the values contained
|
||||
in the message payload that is received over the channel to the adapter.
|
||||
|
||||
The following are used to configure the gateway
|
||||
* The request and reply channels
|
||||
* The data source for the database
|
||||
* The the update/insert statement to be executed.
|
||||
* Optional request SQL Parameter source factory
|
||||
* The select query to be executed after the insert is done
|
||||
* Optional reply SQL Parameter source factory
|
||||
* RowMapper if you intend to map the ResultSet to your custom object
|
||||
|
||||
The following sequence of events happen when we invoke the createPerson method on the gateway
|
||||
* The parameter of type Person is sent as a payload of a message over the reply-channel
|
||||
* The outbound gateway reads this message and extracts the payload
|
||||
* It then executes the given insert statement, the values to be inserted are derived from the payload
|
||||
request-sql-parameter-source-factory provided is used to generate a parameter source.
|
||||
* Since we expect an identity column to be generated, keys-generated is set to true
|
||||
* The result of the insert is then use to create a reply sql parameter source,
|
||||
the factory reply-sql-parameter-source-factory generates the required parameter source
|
||||
* The select query provided is executed
|
||||
* The ResultMapper is used to convert this ResultSet to a Person object
|
||||
* The Person object is then sent as a payload of the Message over the reply channel.
|
||||
* The Person payload is extracted from the Message and returned to the calling application.
|
||||
|
||||
For executing the program and see the results, execute the junit test case
|
||||
org.springframework.integration.samples.jdbc.OutboundGatewayTest
|
||||
|
||||
# Resources
|
||||
|
||||
For help please take a look at the Spring Integration documentation:
|
||||
|
||||
@@ -1,62 +0,0 @@
|
||||
The project contains sample JDBC adapters using Spring integration
|
||||
JDBC Adapters are used to connectthe database systems from Spring integration application
|
||||
|
||||
|
||||
###########################Setting up derby###########################
|
||||
|
||||
If you intend to use some other database, skip this section and goto outbound channel adapters section
|
||||
|
||||
1. Goto http://db.apache.org/derby/derby_downloads.html and download the latest version of derby DB.
|
||||
When this sample was developed 10.8.2.2 was the latest version. If you however choose to use
|
||||
a newer or older version, update the property value for property "derbyclient.driver.version" in pom.xml
|
||||
to the right version you are using.
|
||||
|
||||
2. Extract the downloaded jar file
|
||||
|
||||
3. Go to folder bin in the extracted derby folder and execute startNetworkServer (.bat for windows)
|
||||
This should start the Derby server. By default it starts listening on port 1527
|
||||
|
||||
4. The simple in built client "ij" can be used to create the required table in derby.
|
||||
|
||||
5. Execute ij (.bat for windows)
|
||||
|
||||
6. run the command "connect 'jdbc:derby://localhost:1527/TestDatabase;create=true';" for first time
|
||||
subequently use "connect 'jdbc:derby://localhost:1527/TestDatabase';"
|
||||
|
||||
7. Execute the create table script from the file Person_Derby.sql
|
||||
|
||||
The above steps should setup derby for use.
|
||||
You can use ij to execute the select commands to query the table data.
|
||||
|
||||
|
||||
We have the following examples in this particular Project
|
||||
|
||||
###########################Outbound Channel Adapter######################################
|
||||
|
||||
This adapter is used to insert/update records in a table based on the values contained
|
||||
in the message payload that is received over the channel to the adapter.
|
||||
|
||||
The following are necessary for configuring the adapter
|
||||
1. The datasource or the JDBC template
|
||||
2. The the update/insert query to be executed.
|
||||
3. Optional SQL Parameter source factory
|
||||
|
||||
In our config file "jdbcOutboundAdapterConfig.xml", we have defined an outbound-channel-adapter
|
||||
for the datasource of derby database. Some instructions given to setup derby database.
|
||||
You are free to use any other databae, however you need to appropriately change the datasource
|
||||
details defined in the config
|
||||
|
||||
The adapter does the following.
|
||||
1. It creates a message with payload of type org.springframework.integration.samples.jdbc.Person
|
||||
2. This message is sent over the channel outboundJdbcChannelOne.
|
||||
3. Messages over this channel are delivered to the outbound-channel-adapter to execute the given insert.
|
||||
4. The provided org.springframework.integration.jdbc.ExpressionEvaluatingSqlParameterSourceFactory
|
||||
is used to get an instance of org.springframework.jdbc.core.namedparam.SqlParameterSource. That is
|
||||
used to resolve the given named parameter to the value. In our case the SpEL is executed to yield
|
||||
a value that will be inserted. See the config for more details.
|
||||
|
||||
For executing the program and see the results, execute the junit test case
|
||||
org.springframework.integration.samples.jdbc.OutboundChannelAdapterTest
|
||||
|
||||
Query the database for table Person to see the result.
|
||||
|
||||
@@ -15,8 +15,6 @@
|
||||
<spring.integration.version>2.1.0.M3</spring.integration.version>
|
||||
<slf4j.version>1.6.1</slf4j.version>
|
||||
<junit.version>4.7</junit.version>
|
||||
<spring.test.version>3.0.6.RELEASE</spring.test.version>
|
||||
<derbyclient.driver.version>10.8.2.2</derbyclient.driver.version>
|
||||
</properties>
|
||||
|
||||
<repositories>
|
||||
@@ -125,20 +123,7 @@
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>1.3.160</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Derby dependency -->
|
||||
<dependency>
|
||||
<groupId>org.apache.derby</groupId>
|
||||
<artifactId>derbyclient</artifactId>
|
||||
<version>${derbyclient.driver.version}</version>
|
||||
</dependency>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring test -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${spring.test.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -15,6 +15,10 @@
|
||||
*/
|
||||
package org.springframework.integration.samples.jdbc;
|
||||
|
||||
import java.util.EnumSet;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Represents the gender of the person
|
||||
* @author Amol Nayak
|
||||
@@ -23,6 +27,7 @@ package org.springframework.integration.samples.jdbc;
|
||||
public enum Gender {
|
||||
|
||||
MALE("M"),FEMALE("F");
|
||||
private static Map<String, Gender> map;
|
||||
|
||||
private String identifier;
|
||||
|
||||
@@ -33,4 +38,15 @@ public enum Gender {
|
||||
public String getIdentifier() {
|
||||
return identifier;
|
||||
}
|
||||
|
||||
public static Gender getGenderByIdentifier(String identifier) {
|
||||
return map.get(identifier);
|
||||
}
|
||||
|
||||
static {
|
||||
map = new HashMap<String, Gender>();
|
||||
for(Gender gender:EnumSet.allOf(Gender.class)) {
|
||||
map.put(gender.getIdentifier(), gender);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,11 +24,28 @@ import java.util.Date;
|
||||
*/
|
||||
public class Person {
|
||||
|
||||
private int personId;
|
||||
private String name;
|
||||
private Gender gender;
|
||||
private Date dateOfBirth;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Sets the person id
|
||||
* @return
|
||||
*/
|
||||
public int getPersonId() {
|
||||
return personId;
|
||||
}
|
||||
/**
|
||||
* Get the person Id
|
||||
* @param personId
|
||||
*/
|
||||
public void setPersonId(int personId) {
|
||||
this.personId = personId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the name of the person
|
||||
* @return
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
<int:channel id="replyChannel"/>
|
||||
|
||||
<jdbc:embedded-database id="datasource" type="H2">
|
||||
<jdbc:script location="classpath:setup-tables.sql"/>
|
||||
<jdbc:script location="classpath:setup-tables.sql"/>
|
||||
</jdbc:embedded-database>
|
||||
|
||||
<!-- See also:
|
||||
@@ -34,5 +34,49 @@
|
||||
</int-jdbc:outbound-gateway>
|
||||
|
||||
<bean id="rowMapper" class="org.springframework.integration.samples.jdbc.UserMapper"/>
|
||||
|
||||
<!-- ====================Config for Person Service ========================= -->
|
||||
|
||||
<int:channel id="outboundJdbcRequestChannel"/>
|
||||
<int:channel id="outboundJdbcResponseChannel"/>
|
||||
|
||||
<int:gateway id="personService" service-interface="org.springframework.integration.samples.jdbc.service.PersonService">
|
||||
<int:method name="createPerson"
|
||||
request-channel="outboundJdbcRequestChannel"
|
||||
request-timeout="5000"
|
||||
reply-channel="outboundJdbcResponseChannel"
|
||||
reply-timeout="5000"/>
|
||||
</int:gateway>
|
||||
|
||||
<bean id="personResultMapper" class="org.springframework.integration.samples.jdbc.PersonMapper"/>
|
||||
<int-jdbc:outbound-gateway data-source="datasource"
|
||||
request-channel="outboundJdbcRequestChannel"
|
||||
reply-channel="outboundJdbcResponseChannel"
|
||||
update="insert into Person (name,gender,dateOfBirth)
|
||||
values
|
||||
(:name,:gender,:dateOfBirth)"
|
||||
query="select * from Person where id = :id"
|
||||
request-sql-parameter-source-factory="requestSource"
|
||||
reply-sql-parameter-source-factory="replySource"
|
||||
row-mapper="personResultMapper"
|
||||
keys-generated="true"/>
|
||||
|
||||
<bean id="replySource" class="org.springframework.integration.jdbc.ExpressionEvaluatingSqlParameterSourceFactory">
|
||||
<property name="parameterExpressions">
|
||||
<map>
|
||||
<entry key="id" value="#this['SCOPE_IDENTITY()']"/>
|
||||
</map>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="requestSource" class="org.springframework.integration.jdbc.ExpressionEvaluatingSqlParameterSourceFactory">
|
||||
<property name="parameterExpressions">
|
||||
<map>
|
||||
<entry key="name" value="payload.name.toUpperCase()"/>
|
||||
<entry key="gender" value="payload.gender.identifier"/>
|
||||
<entry key="dateOfBirth" value="payload.dateOfBirth"/>
|
||||
</map>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
create table Person (
|
||||
id integer primary key not null generated always as identity (start with 1, increment by 1),
|
||||
name varchar(100) not null,
|
||||
gender varchar(1) not null,
|
||||
dateOfBirth date
|
||||
);
|
||||
@@ -1,5 +1,6 @@
|
||||
create table IF NOT EXISTS USERS(USERNAME varchar(100),PASSWORD varchar(100), EMAIL varchar(100));
|
||||
create table IF NOT EXISTS DUMMY(DUMMY_VALUE varchar(10));
|
||||
create table IF NOT EXISTS Person(id integer identity primary key , name varchar(100), gender varchar(1), dateOfBirth date);
|
||||
INSERT INTO USERS(USERNAME, PASSWORD, EMAIL) VALUES ('a', 'secret', 'spring-integration@awesome.com');
|
||||
INSERT INTO USERS(USERNAME, PASSWORD, EMAIL) VALUES ('b', 's3cr3t', 'spring@rocks.com');
|
||||
INSERT INTO USERS(USERNAME, PASSWORD, EMAIL) VALUES ('foo', 'bar', 'foo@bar.de');
|
||||
@@ -1,61 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2011 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.samples.jdbc;
|
||||
|
||||
import java.util.Calendar;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.integration.Message;
|
||||
import org.springframework.integration.MessageChannel;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* The test class for jdbc outbound channel adapter
|
||||
* @author Amol Nayak
|
||||
*
|
||||
*/
|
||||
@ContextConfiguration("classpath:jdbcOutboundAdapterConfig.xml")
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
public class OutboundChannelAdapterTest {
|
||||
|
||||
private Logger logger = Logger.getLogger(OutboundChannelAdapterTest.class);
|
||||
@Autowired
|
||||
@Qualifier("outboundJdbcChannelOne")
|
||||
private MessageChannel channel;
|
||||
|
||||
@Test
|
||||
public void insertPersonRecord() {
|
||||
|
||||
logger.info("Creating person Instance");
|
||||
Person person = new Person();
|
||||
Calendar dateOfBirth = Calendar.getInstance();
|
||||
dateOfBirth.set(1980, 0, 1);
|
||||
person.setDateOfBirth(dateOfBirth.getTime());
|
||||
person.setName("Name Of The Person");
|
||||
person.setGender(Gender.MALE);
|
||||
Message<Person> message = MessageBuilder.withPayload(person).build();
|
||||
channel.send(message);
|
||||
logger.info("Sent person Instance");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
<?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:integration="http://www.springframework.org/schema/integration"
|
||||
xmlns:jdbc="http://www.springframework.org/schema/integration/jdbc"
|
||||
xmlns:task="http://www.springframework.org/schema/task"
|
||||
xsi:schemaLocation=
|
||||
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
|
||||
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
|
||||
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.0.xsd
|
||||
http://www.springframework.org/schema/integration/jdbc http://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc-2.0.xsd">
|
||||
|
||||
|
||||
<integration:channel id="outboundJdbcChannelOne"/>
|
||||
|
||||
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
|
||||
<property name="driverClassName" value="org.apache.derby.client.ClientXAConnection"/>
|
||||
<property name="url" value="jdbc:derby://localhost:1527/TestDatabase"/>
|
||||
</bean>
|
||||
|
||||
<jdbc:outbound-channel-adapter data-source="dataSource"
|
||||
channel="outboundJdbcChannelOne"
|
||||
query="insert into Person (name,gender,dateOfBirth)
|
||||
values
|
||||
(:name,:gender,:dateOfBirth)"
|
||||
sql-parameter-source-factory="spelSource"/>
|
||||
|
||||
|
||||
<bean id="spelSource" class="org.springframework.integration.jdbc.ExpressionEvaluatingSqlParameterSourceFactory">
|
||||
<property name="parameterExpressions">
|
||||
<map>
|
||||
<entry key="name" value="payload.name.toUpperCase()"/>
|
||||
<entry key="gender" value="payload.gender.identifier"/>
|
||||
<entry key="dateOfBirth" value="payload.dateOfBirth"/>
|
||||
</map>
|
||||
</property>
|
||||
</bean>
|
||||
</beans>
|
||||
@@ -1,8 +0,0 @@
|
||||
log4j.rootCategory=DEBUG, stdout
|
||||
|
||||
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
|
||||
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
|
||||
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c{2}:%L - %m%n
|
||||
|
||||
|
||||
log4j.category.org.springframework=DEBUG
|
||||
Reference in New Issue
Block a user