Refactoring samples
Restructuring the samples repository Add more docker support Add acceptance tests for the apps Adding sensor average processor sample Remove aggregate samples
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright 2018 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 demo;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* @author Soby Chacko
|
||||
*/
|
||||
@ConfigurationProperties("jdbc")
|
||||
public class JdbcSourceProperties {
|
||||
|
||||
/**
|
||||
* The query to use to select data.
|
||||
*/
|
||||
private String query;
|
||||
|
||||
/**
|
||||
* An SQL update statement to execute for marking polled messages as 'seen'.
|
||||
*/
|
||||
private String update;
|
||||
|
||||
/**
|
||||
* trigger delay for polling
|
||||
*/
|
||||
private long triggerDelay = 1L;
|
||||
|
||||
public String getQuery() {
|
||||
return query;
|
||||
}
|
||||
|
||||
public void setQuery(String query) {
|
||||
this.query = query;
|
||||
}
|
||||
|
||||
public long getTriggerDelay() {
|
||||
return triggerDelay;
|
||||
}
|
||||
|
||||
public void setTriggerDelay(long triggerDelay) {
|
||||
this.triggerDelay = triggerDelay;
|
||||
}
|
||||
|
||||
public String getUpdate() {
|
||||
return update;
|
||||
}
|
||||
|
||||
public void setUpdate(String update) {
|
||||
this.update = update;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
/*
|
||||
* Copyright 2018 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 demo;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.cloud.stream.annotation.EnableBinding;
|
||||
import org.springframework.cloud.stream.annotation.StreamListener;
|
||||
import org.springframework.cloud.stream.messaging.Sink;
|
||||
import org.springframework.cloud.stream.messaging.Source;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.integration.core.MessageSource;
|
||||
import org.springframework.integration.dsl.IntegrationFlow;
|
||||
import org.springframework.integration.dsl.IntegrationFlowBuilder;
|
||||
import org.springframework.integration.dsl.IntegrationFlows;
|
||||
import org.springframework.integration.jdbc.JdbcPollingChannelAdapter;
|
||||
import org.springframework.integration.scheduling.PollerMetadata;
|
||||
import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
|
||||
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
|
||||
import org.springframework.scheduling.support.PeriodicTrigger;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.sql.DataSource;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @author Soby Chacko
|
||||
*/
|
||||
@EnableBinding(Source.class)
|
||||
@SpringBootApplication
|
||||
@EnableConfigurationProperties({JdbcSourceProperties.class})
|
||||
public class SampleJdbcSource {
|
||||
|
||||
public static void main(String... args){
|
||||
SpringApplication.run(SampleJdbcSource.class, args);
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private JdbcSourceProperties properties;
|
||||
|
||||
@Autowired
|
||||
private DataSource dataSource;
|
||||
|
||||
@Autowired
|
||||
private ResourceLoader resourceLoader;
|
||||
|
||||
@Autowired
|
||||
private Source source;
|
||||
|
||||
@Bean
|
||||
public MessageSource<Object> jdbcMessageSource() {
|
||||
JdbcPollingChannelAdapter jdbcPollingChannelAdapter =
|
||||
new JdbcPollingChannelAdapter(this.dataSource, this.properties.getQuery());
|
||||
jdbcPollingChannelAdapter.setUpdateSql(this.properties.getUpdate());
|
||||
return jdbcPollingChannelAdapter;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public IntegrationFlow pollingFlow() {
|
||||
IntegrationFlowBuilder flowBuilder = IntegrationFlows.from(jdbcMessageSource());
|
||||
flowBuilder.channel(this.source.output());
|
||||
return flowBuilder.get();
|
||||
}
|
||||
|
||||
@Bean(
|
||||
name = {"defaultPoller", "org.springframework.integration.context.defaultPollerMetadata"}
|
||||
)
|
||||
public PollerMetadata defaultPoller() {
|
||||
PollerMetadata pollerMetadata = new PollerMetadata();
|
||||
PeriodicTrigger trigger = new PeriodicTrigger(this.properties.getTriggerDelay(), TimeUnit.SECONDS);
|
||||
pollerMetadata.setTrigger(trigger);
|
||||
pollerMetadata.setMaxMessagesPerPoll(1L);
|
||||
return pollerMetadata;
|
||||
}
|
||||
|
||||
//Following method populates the database with some test data
|
||||
@PostConstruct
|
||||
public void initializeData(){
|
||||
ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
|
||||
populator.addScript(resourceLoader.getResource("classpath:sample-schema.sql"));
|
||||
populator.setContinueOnError(true);
|
||||
DatabasePopulatorUtils.execute(populator, dataSource);
|
||||
}
|
||||
|
||||
//Following sink is used as a test consumer. It logs the data received through the consumer.
|
||||
@EnableBinding(Sink.class)
|
||||
static class TestSink {
|
||||
|
||||
private final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
@StreamListener(Sink.INPUT)
|
||||
public void receive(List<Object> list) {
|
||||
logger.info("Data received..." + list);
|
||||
//System.out.println("Data received..." + list);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
spring:
|
||||
datasource:
|
||||
url: jdbc:mariadb://localhost:3306/sample_mysql_db
|
||||
username: root
|
||||
password: pwd
|
||||
driver-class-name: org.mariadb.jdbc.Driver
|
||||
jdbc:
|
||||
query: select id, name, tag from test order by id
|
||||
triggerDelay: 5
|
||||
spring.cloud.stream.bindings.output.destination: test-data
|
||||
spring.cloud.stream.bindings.input.destination: test-data
|
||||
@@ -0,0 +1,9 @@
|
||||
DROP TABLE test;
|
||||
create table test(
|
||||
id bigint,
|
||||
name varchar (2000),
|
||||
tag char(1)
|
||||
);
|
||||
insert into test values (1, 'Bob', NULL);
|
||||
insert into test values (2, 'Jane', NULL);
|
||||
insert into test values (3, 'John', NULL);
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright 2015 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 demo;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(
|
||||
webEnvironment = SpringBootTest.WebEnvironment.NONE)
|
||||
public class ModuleApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
spring.datasource.driver-class-name=org.h2.Driver
|
||||
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=sa
|
||||
Reference in New Issue
Block a user