├── .gitignore ├── README.md ├── file ├── README.md └── file-read-directory │ ├── pom.xml │ └── src │ ├── main │ └── java │ │ └── xpadro │ │ └── spring │ │ └── integration │ │ ├── FileReadDirectoryApplication.java │ │ ├── filter │ │ └── LastModifiedFileFilter.java │ │ └── processor │ │ └── FileProcessor.java │ └── test │ └── java │ └── xpadro │ └── spring │ └── integration │ └── FileReadDirectoryApplicationTests.java ├── generic ├── README.md ├── int-error │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ ├── log4j.xml │ │ │ └── xpadro │ │ │ │ └── spring │ │ │ │ └── integration │ │ │ │ ├── activator │ │ │ │ ├── OrderErrorHandler.java │ │ │ │ └── OrderProcessor.java │ │ │ │ ├── exception │ │ │ │ └── InvalidOrderException.java │ │ │ │ ├── model │ │ │ │ ├── Order.java │ │ │ │ └── OrderConfirmation.java │ │ │ │ ├── router │ │ │ │ └── OrderRouter.java │ │ │ │ └── service │ │ │ │ └── OrderService.java │ │ └── resources │ │ │ ├── db │ │ │ └── schemas │ │ │ │ └── schema.sql │ │ │ └── xpadro │ │ │ └── spring │ │ │ └── integration │ │ │ └── config │ │ │ ├── db-config.xml │ │ │ ├── int-async-config.xml │ │ │ └── int-config.xml │ │ └── test │ │ └── java │ │ └── xpadro │ │ └── spring │ │ └── integration │ │ ├── TestKoAsyncErrorHandling.java │ │ ├── TestOkAsyncErrorHandling.java │ │ └── TestSyncErrorHandling.java ├── int-jdbc-stored │ ├── README.md │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ ├── log4j.xml │ │ │ └── xpadro │ │ │ │ └── spring │ │ │ │ └── integration │ │ │ │ ├── gateway │ │ │ │ └── OrderGateway.java │ │ │ │ └── jdbc │ │ │ │ ├── MyRepository.java │ │ │ │ ├── MyRepositoryImpl.java │ │ │ │ └── model │ │ │ │ └── Order.java │ │ └── resources │ │ │ └── xpadro │ │ │ └── spring │ │ │ └── integration │ │ │ └── config │ │ │ ├── int-config.xml │ │ │ └── jdbc-config.xml │ │ └── test │ │ └── java │ │ └── xpadro │ │ └── spring │ │ └── integration │ │ └── jdbc │ │ └── TestJdbcConfig.java └── int-v4-full │ ├── pom.xml │ └── src │ ├── main │ └── java │ │ ├── log4j.xml │ │ └── xpadro │ │ └── spring │ │ └── integration │ │ ├── configuration │ │ ├── InfrastructureConfiguration.java │ │ ├── MongoDBConfiguration.java │ │ └── WebServiceConfiguration.java │ │ ├── endpoint │ │ ├── CourseRequestBuilder.java │ │ ├── CourseResponseHandler.java │ │ └── StoredCoursesFilter.java │ │ ├── gateway │ │ └── CourseService.java │ │ └── ws │ │ └── types │ │ ├── Course.java │ │ ├── GetCourseListRequest.java │ │ ├── GetCourseListResponse.java │ │ ├── GetCourseRequest.java │ │ ├── GetCourseResponse.java │ │ ├── ObjectFactory.java │ │ └── package-info.java │ └── test │ └── java │ └── xpadro │ └── spring │ └── integration │ └── test │ └── TestApp.java ├── http ├── README.md ├── http-dsl │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ ├── log4j.dtd │ │ │ ├── log4j.xml │ │ │ └── xpadro │ │ │ │ └── spring │ │ │ │ └── integration │ │ │ │ └── server │ │ │ │ ├── configuration │ │ │ │ └── InfrastructureConfiguration.java │ │ │ │ ├── handler │ │ │ │ └── PersonEndpoint.java │ │ │ │ ├── model │ │ │ │ └── ServerPerson.java │ │ │ │ └── service │ │ │ │ ├── PersonService.java │ │ │ │ └── PersonServiceImpl.java │ │ └── webapp │ │ │ └── WEB-INF │ │ │ └── web.xml │ │ └── test │ │ └── java │ │ └── xpadro │ │ └── spring │ │ └── integration │ │ └── test │ │ ├── DeleteOperationsTest.java │ │ ├── GetOperationsTest.java │ │ ├── PostOperationsTest.java │ │ ├── PutOperationsTest.java │ │ └── model │ │ └── ClientPerson.java ├── http-dsl7 │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ ├── log4j.dtd │ │ │ ├── log4j.xml │ │ │ └── xpadro │ │ │ │ └── spring │ │ │ │ └── integration │ │ │ │ └── server │ │ │ │ ├── configuration │ │ │ │ └── InfrastructureConfiguration.java │ │ │ │ ├── handler │ │ │ │ └── PersonEndpoint.java │ │ │ │ ├── model │ │ │ │ └── ServerPerson.java │ │ │ │ └── service │ │ │ │ ├── PersonService.java │ │ │ │ └── PersonServiceImpl.java │ │ └── webapp │ │ │ └── WEB-INF │ │ │ └── web.xml │ │ └── test │ │ └── java │ │ └── xpadro │ │ └── spring │ │ └── integration │ │ └── test │ │ ├── DeleteOperationsTest.java │ │ ├── GetOperationsTest.java │ │ ├── PostOperationsTest.java │ │ ├── PutOperationsTest.java │ │ └── model │ │ └── ClientPerson.java └── http-xml │ ├── pom.xml │ └── src │ ├── main │ ├── java │ │ ├── log4j.dtd │ │ ├── log4j.xml │ │ └── xpadro │ │ │ └── spring │ │ │ └── integration │ │ │ └── server │ │ │ ├── handler │ │ │ └── PersonEndpoint.java │ │ │ ├── model │ │ │ └── ServerPerson.java │ │ │ └── service │ │ │ ├── PersonService.java │ │ │ └── PersonServiceImpl.java │ ├── resources │ │ └── xpadro │ │ │ └── spring │ │ │ └── integration │ │ │ └── configuration │ │ │ └── http-inbound-config.xml │ └── webapp │ │ └── WEB-INF │ │ └── web.xml │ └── test │ └── java │ └── xpadro │ └── spring │ └── integration │ └── test │ ├── DeleteOperationsTest.java │ ├── GetOperationsTest.java │ ├── PostOperationsTest.java │ ├── PutOperationsTest.java │ └── model │ └── ClientPerson.java ├── mongodb ├── README.md ├── mongo-basic │ ├── .gitignore │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── xpadro │ │ │ │ └── spring │ │ │ │ └── integration │ │ │ │ └── mongodb │ │ │ │ ├── MongodbBasicApplication.java │ │ │ │ ├── configuration │ │ │ │ └── InfrastructureConfiguration.java │ │ │ │ ├── endpoint │ │ │ │ ├── OrderToProductConverter.java │ │ │ │ └── ProductProcessor.java │ │ │ │ ├── entity │ │ │ │ ├── Order.java │ │ │ │ └── Product.java │ │ │ │ └── repository │ │ │ │ └── ProductRepository.java │ │ └── resources │ │ │ └── application.properties │ │ └── test │ │ └── java │ │ └── xpadro │ │ └── spring │ │ └── integration │ │ └── mongodb │ │ └── MongodbBasicApplicationTests.java └── mongo-gateway │ ├── .gitignore │ ├── pom.xml │ └── src │ └── main │ ├── java │ └── xpadro │ │ └── spring │ │ └── integration │ │ └── mongodb │ │ ├── example │ │ ├── AbstractApplication.java │ │ ├── JavaConfigQueryApplication.java │ │ ├── JavaDSLQueryApplication.java │ │ ├── JavaDSLQueryExpressionApplication.java │ │ ├── JavaDSLQueryExpressionListApplication.java │ │ ├── configuration │ │ │ ├── JavaConfigQueryConfiguration.java │ │ │ ├── JavaDSLQueryConfiguration.java │ │ │ ├── JavaDSLQueryExpressionConfiguration.java │ │ │ └── JavaDSLQueryExpressionListConfiguration.java │ │ └── domain │ │ │ ├── Person.java │ │ │ ├── PersonRepository.java │ │ │ ├── RequestMessage.java │ │ │ ├── ResultHandler.java │ │ │ └── ResultServiceActivator.java │ │ └── gateway │ │ ├── MongoDb.java │ │ ├── MongoDbExecutor.java │ │ ├── MongoDbOutboundGateway.java │ │ ├── MongoDbOutboundGatewayFactoryBean.java │ │ └── MongoDbOutboundGatewaySpec.java │ └── resources │ └── application.properties ├── rmi ├── README.md └── rmi-basic │ ├── pom.xml │ └── src │ ├── main │ ├── java │ │ └── xpadro │ │ │ └── spring │ │ │ └── integration │ │ │ ├── launcher │ │ │ └── ServerLauncher.java │ │ │ ├── model │ │ │ └── Employee.java │ │ │ ├── repository │ │ │ ├── EmployeeRepository.java │ │ │ └── EmployeeRepositoryImpl.java │ │ │ └── service │ │ │ ├── EmployeeService.java │ │ │ └── EmployeeServiceImpl.java │ └── resources │ │ ├── db │ │ └── schemas │ │ │ ├── data.sql │ │ │ └── schema.sql │ │ └── xpadro │ │ └── spring │ │ └── integration │ │ └── config │ │ └── server-config.xml │ └── test │ ├── java │ └── xpadro │ │ └── spring │ │ └── integration │ │ └── client │ │ ├── TestRmiClient.java │ │ └── TestRmiGatewayClient.java │ └── resources │ └── xpadro │ └── spring │ └── integration │ └── test │ └── config │ ├── client-config.xml │ └── client-gateway-config.xml ├── spring-jms ├── README.md ├── jms-basic │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ ├── log4j.xml │ │ │ └── xpadro │ │ │ │ └── spring │ │ │ │ └── jms │ │ │ │ ├── model │ │ │ │ └── Notification.java │ │ │ │ ├── producer │ │ │ │ └── Producer.java │ │ │ │ └── receiver │ │ │ │ ├── AsyncReceiver.java │ │ │ │ ├── AsyncTopicBarReceiver.java │ │ │ │ ├── AsyncTopicFooReceiver.java │ │ │ │ ├── DynamicTopicReceiver.java │ │ │ │ ├── NotificationRegistry.java │ │ │ │ └── SyncReceiver.java │ │ └── resources │ │ │ └── xpadro │ │ │ └── spring │ │ │ └── jms │ │ │ └── config │ │ │ ├── app-config.xml │ │ │ ├── jms-config.xml │ │ │ └── jms-iter-config.xml │ │ └── test │ │ └── java │ │ └── xpadro │ │ └── spring │ │ └── jms │ │ └── test │ │ ├── TestAsyncMessaging.java │ │ ├── TestDynamicTopicReceiver.java │ │ ├── TestIterateToTopic.java │ │ ├── TestSyncMessaging.java │ │ └── TestTopicMessaging.java ├── jms-boot-javaconfig │ ├── activemq-data │ │ └── embedded │ │ │ └── KahaDB │ │ │ ├── db-1.log │ │ │ ├── db.data │ │ │ └── db.redo │ ├── pom.xml │ └── src │ │ ├── main │ │ └── java │ │ │ └── xpadro │ │ │ └── spring │ │ │ └── jms │ │ │ ├── JmsJavaconfigApplication.java │ │ │ ├── listener │ │ │ ├── InListener.java │ │ │ ├── OutListener.java │ │ │ └── SimpleListener.java │ │ │ ├── model │ │ │ └── Order.java │ │ │ └── service │ │ │ ├── ClientService.java │ │ │ ├── ClientServiceImpl.java │ │ │ ├── RegisterService.java │ │ │ ├── RegisterServiceImpl.java │ │ │ ├── StoreService.java │ │ │ └── StoreServiceImpl.java │ │ └── test │ │ └── java │ │ └── xpadro │ │ └── spring │ │ └── jms │ │ ├── InOutListenerTest.java │ │ └── SimpleListenerTest.java └── jms-tx │ ├── pom.xml │ └── src │ ├── main │ ├── java │ │ ├── log4j.xml │ │ └── xpadro │ │ │ └── spring │ │ │ └── jms │ │ │ ├── model │ │ │ └── Notification.java │ │ │ ├── producer │ │ │ └── Producer.java │ │ │ └── receiver │ │ │ └── NotificationProcessor.java │ └── resources │ │ ├── db │ │ └── schema.sql │ │ └── xpadro │ │ └── spring │ │ └── jms │ │ └── config │ │ ├── app-config.xml │ │ ├── notx-jms-config.xml │ │ └── tx-jms-config.xml │ └── test │ └── java │ └── xpadro │ └── spring │ └── jms │ └── test │ ├── TestBaseMessaging.java │ ├── TestNotTransactedMessaging.java │ └── TestTransactedMessaging.java └── webservices ├── README.md ├── spring-ws ├── generate-classes.xml ├── generate-schema.xml ├── lib │ └── trang.jar ├── pom.xml └── src │ ├── main │ ├── java │ │ └── xpadro │ │ │ └── spring │ │ │ └── ws │ │ │ ├── OrderEndpoint.java │ │ │ ├── exception │ │ │ ├── ClientNotFoundException.java │ │ │ └── OrderFormatDateException.java │ │ │ ├── model │ │ │ └── OrderConfirmation.java │ │ │ ├── service │ │ │ ├── OrderService.java │ │ │ └── impl │ │ │ │ └── StubOrderService.java │ │ │ └── types │ │ │ ├── ClientDataRequest.java │ │ │ ├── ClientDataResponse.java │ │ │ ├── ObjectFactory.java │ │ │ └── package-info.java │ ├── resources │ │ └── xpadro │ │ │ └── spring │ │ │ └── ws │ │ │ └── config │ │ │ ├── root-config.xml │ │ │ └── servlet-config.xml │ └── webapp │ │ └── WEB-INF │ │ ├── schemas │ │ ├── samples │ │ │ ├── client-request.xml │ │ │ └── client-response.xml │ │ └── xsd │ │ │ ├── client-request.xsd │ │ │ ├── client-response.xsd │ │ │ └── client-service.xsd │ │ └── web.xml │ └── test │ ├── java │ └── xpadro │ │ └── spring │ │ └── ws │ │ └── test │ │ ├── TestClient.java │ │ └── TestWebService.java │ └── resources │ └── xpadro │ └── spring │ └── ws │ └── test │ └── config │ ├── client-config.xml │ └── test-server-config.xml ├── ws-retry-adv ├── pom.xml └── src │ ├── main │ └── java │ │ ├── log4j.xml │ │ └── xpadro │ │ └── spring │ │ └── integration │ │ ├── activator │ │ └── ClientServiceActivator.java │ │ ├── gateway │ │ └── ClientService.java │ │ ├── interceptor │ │ └── ServiceClientInterceptor.java │ │ └── types │ │ ├── ClientDataRequest.java │ │ ├── ClientDataResponse.java │ │ ├── ObjectFactory.java │ │ └── package-info.java │ └── test │ ├── java │ └── xpadro │ │ └── spring │ │ └── integration │ │ └── TestInvocation.java │ └── resources │ └── xpadro │ └── spring │ └── integration │ └── config │ ├── int-config.xml │ └── mongodb-config.xml ├── ws-retry ├── pom.xml └── src │ ├── main │ └── java │ │ ├── log4j.xml │ │ └── xpadro │ │ └── spring │ │ └── integration │ │ ├── activator │ │ └── ClientServiceActivator.java │ │ ├── data │ │ └── RequestData.java │ │ ├── gateway │ │ └── ClientService.java │ │ ├── router │ │ └── ServiceResultRouter.java │ │ ├── trigger │ │ └── ServiceRetryTrigger.java │ │ └── types │ │ ├── ClientDataRequest.java │ │ ├── ClientDataResponse.java │ │ ├── ObjectFactory.java │ │ └── package-info.java │ └── test │ ├── java │ └── xpadro │ │ └── spring │ │ └── integration │ │ └── TestInvocation.java │ └── resources │ └── xpadro │ └── spring │ └── integration │ └── config │ ├── int-config.xml │ └── mongodb-config.xml └── ws-timeout ├── pom.xml └── src ├── main ├── java │ └── xpadro │ │ └── spring │ │ └── integration │ │ └── ws │ │ ├── gateway │ │ └── CourseService.java │ │ └── types │ │ ├── Course.java │ │ ├── GetCourseListRequest.java │ │ ├── GetCourseListResponse.java │ │ ├── GetCourseRequest.java │ │ ├── GetCourseResponse.java │ │ ├── ObjectFactory.java │ │ └── package-info.java └── resources │ ├── props │ └── service.properties │ └── xpadro │ └── spring │ └── integration │ └── ws │ └── config │ └── int-course-config.xml └── test └── java └── xpadro └── spring └── integration └── ws └── test └── TestIntegrationApp.java /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | *.iml 3 | target/ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # spring-integration 2 | Samples of different Spring Integration modules (jms, batch, integration). These modules are split into the following sections: 3 | 4 | * [HTTP] - Processing of messages over HTTP. 5 | 6 | * [WEB SERVICES] - Examples using Spring Integration Web Services support. 7 | 8 | * [MONGO DB] - Integration with a MongoDB database. 9 | 10 | * [RMI] - Processing of messages over RMI. 11 | 12 | * [SPRING JMS] - Although not a direct part of the Spring Integration project, it is included in the certification guide. For this reason, it is appropriate to include this module in this repository. 13 | 14 | * [FILE] - Support for file processing. 15 | 16 | * [GENERIC] - Generic examples which are not specific to any of the previous sections. 17 | 18 | Inside each section there is another readme file with a list of all related project examples. Some of them have a link to a blog entry explaining in detail how its components work. 19 | 20 | [WEB SERVICES]: https://github.com/xpadro/spring-integration/tree/master/webservices 21 | [SPRING JMS]: https://github.com/xpadro/spring-integration/tree/master/spring-jms 22 | [HTTP]: https://github.com/xpadro/spring-integration/tree/master/http 23 | [MONGO DB]: https://github.com/xpadro/spring-integration/tree/master/mongodb 24 | [RMI]: https://github.com/xpadro/spring-integration/tree/master/rmi 25 | [FILE]: https://github.com/xpadro/spring-integration/tree/master/file 26 | [GENERIC]: https://github.com/xpadro/spring-integration/tree/master/generic 27 | -------------------------------------------------------------------------------- /file/README.md: -------------------------------------------------------------------------------- 1 | # FILE 2 | Spring Integration File support 3 | #### Version 4 4 | Examples using Spring Integration 4 release: 5 | 6 | * [file-read] - This sample polls a specific directory from the file system and processes all new or modified files. 7 | * Blog post: http://xpadro.com/2016/07/spring-integration-polling-file.html 8 | 9 | 10 | [file-read]: https://github.com/xpadro/spring-integration/tree/master/file/file-read-directory 11 | 12 | -------------------------------------------------------------------------------- /file/file-read-directory/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | xpadro.spring.integration 7 | file-read-directory 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | file-read-directory 12 | Demo project for Spring Boot 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 1.3.5.RELEASE 18 | 19 | 20 | 21 | 22 | UTF-8 23 | 1.8 24 | 25 | 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter-integration 30 | 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-test 35 | test 36 | 37 | 38 | 39 | 40 | org.springframework.integration 41 | spring-integration-java-dsl 42 | 1.0.0.RELEASE 43 | 44 | 45 | 46 | 47 | commons-io 48 | commons-io 49 | 2.5 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | org.springframework.boot 58 | spring-boot-maven-plugin 59 | 60 | 61 | 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /file/file-read-directory/src/main/java/xpadro/spring/integration/filter/LastModifiedFileFilter.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.filter; 2 | 3 | import org.springframework.integration.file.filters.AbstractFileListFilter; 4 | 5 | import java.io.File; 6 | import java.util.HashMap; 7 | import java.util.Map; 8 | 9 | public class LastModifiedFileFilter extends AbstractFileListFilter { 10 | private final Map files = new HashMap<>(); 11 | private final Object monitor = new Object(); 12 | 13 | @Override 14 | protected boolean accept(File file) { 15 | synchronized (this.monitor) { 16 | Long previousModifiedTime = files.put(file.getName(), file.lastModified()); 17 | 18 | return previousModifiedTime == null || previousModifiedTime != file.lastModified(); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /file/file-read-directory/src/main/java/xpadro/spring/integration/processor/FileProcessor.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.processor; 2 | 3 | import org.springframework.messaging.Message; 4 | 5 | public class FileProcessor { 6 | private static final String HEADER_FILE_NAME = "file_name"; 7 | private static final String MSG = "%s received. Content: %s"; 8 | 9 | public void process(Message msg) { 10 | String fileName = (String) msg.getHeaders().get(HEADER_FILE_NAME); 11 | String content = msg.getPayload(); 12 | 13 | System.out.println(String.format(MSG, fileName, content)); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /file/file-read-directory/src/test/java/xpadro/spring/integration/FileReadDirectoryApplicationTests.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.SpringApplicationConfiguration; 6 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 7 | 8 | import java.io.IOException; 9 | 10 | @RunWith(SpringJUnit4ClassRunner.class) 11 | @SpringApplicationConfiguration(classes = FileReadDirectoryApplication.class) 12 | public class FileReadDirectoryApplicationTests { 13 | 14 | @Test 15 | public void contextLoads() throws IOException, InterruptedException { 16 | 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /generic/README.md: -------------------------------------------------------------------------------- 1 | # GENERIC 2 | Generic examples which do not apply to a specific section. 3 | #### Version 4 4 | Examples using Spring Integration 4 release: 5 | 6 | * [int-v4-full] - Implemented with Spring 4.0.3 and Spring Integration 4.0, this project shows an example of configuring an integration application without using XML. The configuration is defined in @Configuration classes. 7 | * Blog post: https://xpadro.com/2014/05/spring-integration-4-0-a-complete-xml-free-example.html 8 | 9 | #### Version 3 10 | Examples using Spring Integration 3 release: 11 | 12 | * [int-jdbc-stored] - This project is configured to call a stored procedure from a MySQL database. 13 | 14 | #### Version 2 15 | Examples using Spring Integration 2 release: 16 | 17 | * [int-error] - Implements and tests the different mechanisms for error handling in Spring Integration. Tests are splitted in two main areas; synchronous and asynchronous messaging. 18 | * Blog post: http://xpadro.com/2013/11/how-error-handling-works-in-spring.html 19 | 20 | 21 | 22 | [int-error]: https://github.com/xpadro/spring-integration/tree/master/generic/int-error 23 | [int-jdbc-stored]: https://github.com/xpadro/spring-integration/tree/master/generic/int-jdbc-stored 24 | [int-v4-full]: https://github.com/xpadro/spring-integration/tree/master/generic/int-v4-full 25 | 26 | -------------------------------------------------------------------------------- /generic/int-error/src/main/java/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /generic/int-error/src/main/java/xpadro/spring/integration/activator/OrderErrorHandler.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.activator; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.integration.Message; 5 | import org.springframework.integration.MessageHandlingException; 6 | import org.springframework.integration.annotation.ServiceActivator; 7 | import org.springframework.jdbc.core.JdbcTemplate; 8 | 9 | import xpadro.spring.integration.model.Order; 10 | 11 | /** 12 | * Handler subscribed to the global error channel. It will store the error to the database 13 | * 14 | * @author xpadro 15 | * 16 | */ 17 | public class OrderErrorHandler { 18 | @Autowired 19 | private JdbcTemplate jdbcTemplate; 20 | 21 | @ServiceActivator 22 | public void handleFailedOrder(Message message) { 23 | Order requestedOrder = (Order) message.getPayload().getFailedMessage().getPayload(); 24 | saveToBD(requestedOrder.getId(), message.getPayload().getMessage()); 25 | } 26 | 27 | private void saveToBD(int orderId, String errorMessage) { 28 | String query = "insert into errors(orderid, message) values (?,?)"; 29 | jdbcTemplate.update(query, orderId, errorMessage); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /generic/int-error/src/main/java/xpadro/spring/integration/activator/OrderProcessor.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.activator; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.stereotype.Component; 6 | 7 | import xpadro.spring.integration.exception.InvalidOrderException; 8 | import xpadro.spring.integration.model.Order; 9 | import xpadro.spring.integration.model.OrderConfirmation; 10 | 11 | /** 12 | * Processor that receives an order and returns an order confirmation 13 | * 14 | * @author xpadro 15 | * 16 | */ 17 | @Component("orderProcessor") 18 | public class OrderProcessor { 19 | private static final Logger logger = LoggerFactory.getLogger(OrderProcessor.class); 20 | private static final String ERROR_INVALID_ID = "Order ID is invalid"; 21 | 22 | public OrderConfirmation processOrder(Order order) { 23 | logger.info("Processing order {}", order.getId()); 24 | 25 | if (isInvalidOrder(order)) { 26 | logger.info("Error while processing order [{}]", ERROR_INVALID_ID); 27 | throw new InvalidOrderException(ERROR_INVALID_ID); 28 | } 29 | 30 | return new OrderConfirmation("confirmed"); 31 | } 32 | 33 | private boolean isInvalidOrder(Order order) { 34 | if (order.getId() == 1 || order.getId() == 6) { 35 | return true; 36 | } 37 | return false; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /generic/int-error/src/main/java/xpadro/spring/integration/exception/InvalidOrderException.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.exception; 2 | 3 | /** 4 | * Exception raised when the order processor cannot process an order 5 | * 6 | * @author xpadro 7 | * 8 | */ 9 | public class InvalidOrderException extends RuntimeException { 10 | private static final long serialVersionUID = 1L; 11 | 12 | public InvalidOrderException() { 13 | super(); 14 | } 15 | 16 | public InvalidOrderException(String message) { 17 | super(message); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /generic/int-error/src/main/java/xpadro/spring/integration/model/Order.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.model; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * Contains detail of orders sent to process 7 | * 8 | * @author xpadro 9 | * 10 | */ 11 | public class Order implements Serializable { 12 | private static final long serialVersionUID = 1L; 13 | 14 | private int id; 15 | private String description; 16 | 17 | public Order() {} 18 | 19 | public Order(int id, String description) { 20 | this.id = id; 21 | this.description = description; 22 | } 23 | 24 | public int getId() { 25 | return id; 26 | } 27 | 28 | public void setId(int id) { 29 | this.id = id; 30 | } 31 | 32 | public String getDescription() { 33 | return description; 34 | } 35 | 36 | public void setDescription(String description) { 37 | this.description = description; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /generic/int-error/src/main/java/xpadro/spring/integration/model/OrderConfirmation.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.model; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * Contains detail of order confirmation generated from request orders 7 | * 8 | * @author xpadro 9 | * 10 | */ 11 | public class OrderConfirmation implements Serializable { 12 | private static final long serialVersionUID = 1L; 13 | 14 | private String id; 15 | 16 | public OrderConfirmation() {} 17 | 18 | public OrderConfirmation(String id) { 19 | this.id = id; 20 | } 21 | 22 | public String getId() { 23 | return id; 24 | } 25 | 26 | public void setId(String id) { 27 | this.id = id; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /generic/int-error/src/main/java/xpadro/spring/integration/router/OrderRouter.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.router; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.stereotype.Component; 6 | 7 | import xpadro.spring.integration.model.Order; 8 | 9 | /** 10 | * Redirects an order to the appropriate channel (sync or async) depending on the order id 11 | * 12 | * @author xpadro 13 | * 14 | */ 15 | @Component("orderRouter") 16 | public class OrderRouter { 17 | private static final Logger logger = LoggerFactory.getLogger(OrderRouter.class); 18 | private static final String ASYNC_CHANNEL = "asyncChannel"; 19 | private static final String SYNC_CHANNEL = "syncChannel"; 20 | 21 | 22 | public String redirectOrder(Order order) { 23 | if (order.getId() < 5) { 24 | logger.info("Redirecting to {}", SYNC_CHANNEL); 25 | return SYNC_CHANNEL; 26 | } 27 | else { 28 | logger.info("Redirecting to {}", ASYNC_CHANNEL); 29 | return ASYNC_CHANNEL; 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /generic/int-error/src/main/java/xpadro/spring/integration/service/OrderService.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.service; 2 | 3 | import java.util.concurrent.Future; 4 | 5 | import org.springframework.integration.annotation.Gateway; 6 | 7 | import xpadro.spring.integration.model.Order; 8 | import xpadro.spring.integration.model.OrderConfirmation; 9 | 10 | /** 11 | * Contains sync and async methods as entry to the messaging system 12 | * 13 | * @author xpadro 14 | * 15 | */ 16 | public interface OrderService { 17 | @Gateway 18 | public OrderConfirmation sendOrder(Order order); 19 | 20 | @Gateway 21 | public Future sendFutureOrder(Order order); 22 | } 23 | -------------------------------------------------------------------------------- /generic/int-error/src/main/resources/db/schemas/schema.sql: -------------------------------------------------------------------------------- 1 | drop table errors if exists; 2 | 3 | create table errors (orderid integer primary key, message varchar(255)); -------------------------------------------------------------------------------- /generic/int-error/src/main/resources/xpadro/spring/integration/config/db-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /generic/int-error/src/main/resources/xpadro/spring/integration/config/int-async-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | 15 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /generic/int-error/src/main/resources/xpadro/spring/integration/config/int-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 12 | 13 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /generic/int-error/src/test/java/xpadro/spring/integration/TestKoAsyncErrorHandling.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration; 2 | 3 | import java.util.concurrent.ExecutionException; 4 | import java.util.concurrent.Future; 5 | 6 | import junit.framework.Assert; 7 | 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.test.context.ContextConfiguration; 12 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 13 | 14 | import xpadro.spring.integration.model.Order; 15 | import xpadro.spring.integration.model.OrderConfirmation; 16 | import xpadro.spring.integration.service.OrderService; 17 | 18 | @ContextConfiguration(locations = {"/xpadro/spring/integration/config/int-config.xml"}) 19 | @RunWith(SpringJUnit4ClassRunner.class) 20 | public class TestKoAsyncErrorHandling { 21 | 22 | @Autowired 23 | private OrderService service; 24 | 25 | @Test 26 | public void testCorrectOrder() throws InterruptedException, ExecutionException { 27 | Future confirmation = service.sendFutureOrder(new Order(7, "another correct order")); 28 | OrderConfirmation orderConfirmation = confirmation.get(); 29 | Assert.assertNotNull(orderConfirmation); 30 | Assert.assertEquals("confirmed", orderConfirmation.getId()); 31 | } 32 | 33 | 34 | /*@Test(expected=MessageHandlingException.class) 35 | * This test will fail, since the exception will never reach the caller 36 | */ 37 | public void testAsyncErrorHandling() throws InterruptedException, ExecutionException { 38 | Future confirmation = service.sendFutureOrder(new Order(6, "another order")); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /generic/int-error/src/test/java/xpadro/spring/integration/TestSyncErrorHandling.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.integration.MessageHandlingException; 8 | import org.springframework.test.context.ContextConfiguration; 9 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 10 | 11 | import xpadro.spring.integration.exception.InvalidOrderException; 12 | import xpadro.spring.integration.model.Order; 13 | import xpadro.spring.integration.model.OrderConfirmation; 14 | import xpadro.spring.integration.service.OrderService; 15 | 16 | @ContextConfiguration(locations = {"/xpadro/spring/integration/config/int-config.xml"}) 17 | @RunWith(SpringJUnit4ClassRunner.class) 18 | public class TestSyncErrorHandling { 19 | 20 | @Autowired 21 | private OrderService service; 22 | 23 | @Test 24 | public void testCorrectOrder() { 25 | OrderConfirmation confirmation = service.sendOrder(new Order(3, "a correct order")); 26 | Assert.assertNotNull(confirmation); 27 | Assert.assertEquals("confirmed", confirmation.getId()); 28 | } 29 | 30 | @Test 31 | public void testSyncErrorHandling() { 32 | OrderConfirmation confirmation = null; 33 | try { 34 | confirmation = service.sendOrder(new Order(1, "an invalid order")); 35 | Assert.fail("Should throw a MessageHandlingException"); 36 | } catch (MessageHandlingException e) { 37 | Assert.assertEquals(InvalidOrderException.class, e.getCause().getClass()); 38 | Assert.assertNull(confirmation); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /generic/int-jdbc-stored/README.md: -------------------------------------------------------------------------------- 1 | This project is configured to call a stored procedure from a MySQL database. The steps to prepare the environment are described below: 2 | 3 | - Select test database 4 | > USE test; 5 | 6 | - Create table orders 7 | > CREATE TABLE orders (orderId integer primary key, description varchar(255)); 8 | 9 | - Create the stored procedure 'get_order' 10 | > DELIMITER // 11 | > CREATE PROCEDURE get_order (IN id INT, OUT descrip VARCHAR(255)) 12 | > BEGIN 13 | > SELECT description INTO descrip FROM orders 14 | > WHERE orderId = id; 15 | > END // 16 | -------------------------------------------------------------------------------- /generic/int-jdbc-stored/src/main/java/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /generic/int-jdbc-stored/src/main/java/xpadro/spring/integration/gateway/OrderGateway.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.gateway; 2 | 3 | public interface OrderGateway { 4 | 5 | public String getOrderName(int orderId); 6 | } 7 | -------------------------------------------------------------------------------- /generic/int-jdbc-stored/src/main/java/xpadro/spring/integration/jdbc/MyRepository.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.jdbc; 2 | 3 | import xpadro.spring.integration.jdbc.model.Order; 4 | 5 | public interface MyRepository { 6 | 7 | Order getOrder(int id); 8 | 9 | String getDescriptionFromStoredProcedure(int id); 10 | } 11 | -------------------------------------------------------------------------------- /generic/int-jdbc-stored/src/main/java/xpadro/spring/integration/jdbc/model/Order.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.jdbc.model; 2 | 3 | public class Order { 4 | 5 | private final int orderId; 6 | private final String description; 7 | 8 | public Order(int orderId, String description) { 9 | this.orderId = orderId; 10 | this.description = description; 11 | } 12 | 13 | public int getOrderId() { 14 | return this.orderId; 15 | } 16 | 17 | public String getDescription() { 18 | return this.description; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /generic/int-jdbc-stored/src/main/resources/xpadro/spring/integration/config/int-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | 15 | 16 | 17 | 19 | 20 | 21 | 22 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /generic/int-jdbc-stored/src/main/resources/xpadro/spring/integration/config/jdbc-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /generic/int-jdbc-stored/src/test/java/xpadro/spring/integration/jdbc/TestJdbcConfig.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.jdbc; 2 | 3 | import junit.framework.Assert; 4 | 5 | import org.junit.Test; 6 | import org.junit.runner.RunWith; 7 | import org.slf4j.Logger; 8 | import org.slf4j.LoggerFactory; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.test.context.ContextConfiguration; 11 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 12 | 13 | import xpadro.spring.integration.gateway.OrderGateway; 14 | import xpadro.spring.integration.jdbc.model.Order; 15 | 16 | 17 | @ContextConfiguration(locations = {"/xpadro/spring/integration/config/int-config.xml"}) 18 | @RunWith(SpringJUnit4ClassRunner.class) 19 | public class TestJdbcConfig { 20 | private static final Logger logger = LoggerFactory.getLogger(TestJdbcConfig.class); 21 | 22 | @Autowired 23 | MyRepository repository; 24 | 25 | @Autowired 26 | OrderGateway gateway; 27 | 28 | 29 | @Test 30 | public void testAccessMySQLDatabase() { 31 | Order order = repository.getOrder(2); 32 | Assert.assertEquals("PK2222", order.getDescription()); 33 | logger.info("Order from JDBC query: {}", order.getDescription()); 34 | } 35 | 36 | @Test 37 | public void testCallStoredProcedure() { 38 | String result = repository.getDescriptionFromStoredProcedure(2); 39 | Assert.assertEquals("PK2222", result); 40 | logger.info("Order from JDBC stored procedure: {}", result); 41 | } 42 | 43 | @Test 44 | public void testIntegration() { 45 | String result = gateway.getOrderName(2); 46 | Assert.assertEquals("PK2222", result); 47 | logger.info("Order from SI Gateway stored procedure: {}", result); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /generic/int-v4-full/src/main/java/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /generic/int-v4-full/src/main/java/xpadro/spring/integration/configuration/InfrastructureConfiguration.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.configuration; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.ComponentScan; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.context.annotation.Description; 7 | import org.springframework.context.annotation.Import; 8 | import org.springframework.integration.annotation.IntegrationComponentScan; 9 | import org.springframework.integration.channel.DirectChannel; 10 | import org.springframework.integration.channel.PublishSubscribeChannel; 11 | import org.springframework.integration.config.EnableIntegration; 12 | import org.springframework.messaging.MessageChannel; 13 | 14 | @Configuration 15 | @ComponentScan("xpadro.spring.integration.endpoint") //@Component 16 | @IntegrationComponentScan("xpadro.spring.integration.gateway") //@MessagingGateway 17 | @EnableIntegration 18 | @Import({MongoDBConfiguration.class, WebServiceConfiguration.class}) 19 | public class InfrastructureConfiguration { 20 | 21 | @Bean 22 | @Description("Entry to the messaging system through the gateway.") 23 | public MessageChannel requestChannel() { 24 | return new DirectChannel(); 25 | } 26 | 27 | @Bean 28 | @Description("Sends request messages to the web service outbound gateway") 29 | public MessageChannel invocationChannel() { 30 | return new DirectChannel(); 31 | } 32 | 33 | @Bean 34 | @Description("Sends web service responses to both the client and a database") 35 | public MessageChannel responseChannel() { 36 | return new PublishSubscribeChannel(); 37 | } 38 | 39 | @Bean 40 | @Description("Stores non filtered messages to the database") 41 | public MessageChannel storeChannel() { 42 | return new DirectChannel(); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /generic/int-v4-full/src/main/java/xpadro/spring/integration/configuration/MongoDBConfiguration.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.configuration; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.data.mongodb.MongoDbFactory; 6 | import org.springframework.data.mongodb.core.SimpleMongoDbFactory; 7 | import org.springframework.expression.common.LiteralExpression; 8 | import org.springframework.integration.annotation.ServiceActivator; 9 | import org.springframework.integration.mongodb.outbound.MongoDbStoringMessageHandler; 10 | import org.springframework.messaging.MessageHandler; 11 | 12 | import com.mongodb.MongoClient; 13 | 14 | @Configuration 15 | public class MongoDBConfiguration { 16 | 17 | @Bean 18 | public MongoDbFactory mongoDbFactory() throws Exception { 19 | return new SimpleMongoDbFactory(new MongoClient(), "si4Db"); 20 | } 21 | 22 | @Bean 23 | @ServiceActivator(inputChannel = "storeChannel") 24 | public MessageHandler mongodbAdapter() throws Exception { 25 | MongoDbStoringMessageHandler adapter = new MongoDbStoringMessageHandler(mongoDbFactory()); 26 | adapter.setCollectionNameExpression(new LiteralExpression("courses")); 27 | 28 | return adapter; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /generic/int-v4-full/src/main/java/xpadro/spring/integration/configuration/WebServiceConfiguration.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.configuration; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.integration.annotation.ServiceActivator; 6 | import org.springframework.integration.ws.MarshallingWebServiceOutboundGateway; 7 | import org.springframework.messaging.MessageHandler; 8 | import org.springframework.oxm.jaxb.Jaxb2Marshaller; 9 | 10 | @Configuration 11 | public class WebServiceConfiguration { 12 | 13 | @Bean 14 | @ServiceActivator(inputChannel = "invocationChannel") 15 | public MessageHandler wsOutboundGateway() { 16 | MarshallingWebServiceOutboundGateway gw = new MarshallingWebServiceOutboundGateway("http://localhost:8080/spring-ws-courses/courses", jaxb2Marshaller()); 17 | gw.setOutputChannelName("responseChannel"); 18 | 19 | return gw; 20 | } 21 | 22 | @Bean 23 | public Jaxb2Marshaller jaxb2Marshaller() { 24 | Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); 25 | marshaller.setContextPath("xpadro.spring.integration.ws.types"); 26 | 27 | return marshaller; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /generic/int-v4-full/src/main/java/xpadro/spring/integration/endpoint/CourseRequestBuilder.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.endpoint; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.integration.annotation.Transformer; 6 | import org.springframework.messaging.Message; 7 | import org.springframework.stereotype.Component; 8 | 9 | import xpadro.spring.integration.ws.types.GetCourseRequest; 10 | 11 | @Component 12 | public class CourseRequestBuilder { 13 | private Logger logger = LoggerFactory.getLogger(this.getClass()); 14 | 15 | @Transformer(inputChannel="requestChannel", outputChannel="invocationChannel") 16 | public GetCourseRequest buildRequest(Message msg) { 17 | logger.info("Building request for course [{}]", msg.getPayload()); 18 | GetCourseRequest request = new GetCourseRequest(); 19 | request.setCourseId(msg.getPayload()); 20 | 21 | return request; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /generic/int-v4-full/src/main/java/xpadro/spring/integration/endpoint/CourseResponseHandler.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.endpoint; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.integration.annotation.ServiceActivator; 6 | import org.springframework.messaging.Message; 7 | import org.springframework.stereotype.Component; 8 | 9 | import xpadro.spring.integration.ws.types.GetCourseResponse; 10 | 11 | @Component 12 | public class CourseResponseHandler { 13 | private Logger logger = LoggerFactory.getLogger(this.getClass()); 14 | 15 | @ServiceActivator(inputChannel="responseChannel") 16 | public String getResponse(Message msg) { 17 | GetCourseResponse course = msg.getPayload(); 18 | logger.info("Course with ID [{}] received: {}", course.getCourseId(), course.getName()); 19 | 20 | return course.getName(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /generic/int-v4-full/src/main/java/xpadro/spring/integration/endpoint/StoredCoursesFilter.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.endpoint; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.integration.annotation.Filter; 6 | import org.springframework.messaging.Message; 7 | import org.springframework.stereotype.Component; 8 | 9 | import xpadro.spring.integration.ws.types.GetCourseResponse; 10 | 11 | @Component 12 | public class StoredCoursesFilter { 13 | private Logger logger = LoggerFactory.getLogger(this.getClass()); 14 | 15 | @Filter(inputChannel="responseChannel", outputChannel="storeChannel") 16 | public boolean filterCourse(Message msg) { 17 | if (!msg.getPayload().getCourseId().startsWith("BC-")) { 18 | logger.info("Course [{}] filtered. Not a BF course", msg.getPayload().getCourseId()); 19 | return false; 20 | } 21 | 22 | logger.info("Course [{}] validated. Storing to database", msg.getPayload().getCourseId()); 23 | return true; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /generic/int-v4-full/src/main/java/xpadro/spring/integration/gateway/CourseService.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.gateway; 2 | 3 | import org.springframework.integration.annotation.MessagingGateway; 4 | 5 | @MessagingGateway(name = "entryGateway", defaultRequestChannel = "requestChannel") 6 | public interface CourseService { 7 | 8 | public String findCourse(String courseId); 9 | } 10 | -------------------------------------------------------------------------------- /generic/int-v4-full/src/main/java/xpadro/spring/integration/ws/types/GetCourseListRequest.java: -------------------------------------------------------------------------------- 1 | // 2 | // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.4-2 3 | // See http://java.sun.com/xml/jaxb 4 | // Any modifications to this file will be lost upon recompilation of the source schema. 5 | // Generated on: 2014.04.24 at 04:39:37 PM CEST 6 | // 7 | 8 | 9 | package xpadro.spring.integration.ws.types; 10 | 11 | import javax.xml.bind.annotation.XmlAccessType; 12 | import javax.xml.bind.annotation.XmlAccessorType; 13 | import javax.xml.bind.annotation.XmlRootElement; 14 | import javax.xml.bind.annotation.XmlType; 15 | 16 | 17 | /** 18 | *

Java class for anonymous complex type. 19 | * 20 | *

The following schema fragment specifies the expected content contained within this class. 21 | * 22 | *

23 |  * <complexType>
24 |  *   <complexContent>
25 |  *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
26 |  *     </restriction>
27 |  *   </complexContent>
28 |  * </complexType>
29 |  * 
30 | * 31 | * 32 | */ 33 | @XmlAccessorType(XmlAccessType.FIELD) 34 | @XmlType(name = "") 35 | @XmlRootElement(name = "getCourseListRequest") 36 | public class GetCourseListRequest { 37 | 38 | 39 | } 40 | -------------------------------------------------------------------------------- /generic/int-v4-full/src/main/java/xpadro/spring/integration/ws/types/GetCourseRequest.java: -------------------------------------------------------------------------------- 1 | // 2 | // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.4-2 3 | // See http://java.sun.com/xml/jaxb 4 | // Any modifications to this file will be lost upon recompilation of the source schema. 5 | // Generated on: 2014.04.24 at 04:39:37 PM CEST 6 | // 7 | 8 | 9 | package xpadro.spring.integration.ws.types; 10 | 11 | import javax.xml.bind.annotation.XmlAccessType; 12 | import javax.xml.bind.annotation.XmlAccessorType; 13 | import javax.xml.bind.annotation.XmlAttribute; 14 | import javax.xml.bind.annotation.XmlRootElement; 15 | import javax.xml.bind.annotation.XmlType; 16 | 17 | 18 | /** 19 | *

Java class for anonymous complex type. 20 | * 21 | *

The following schema fragment specifies the expected content contained within this class. 22 | * 23 | *

24 |  * <complexType>
25 |  *   <complexContent>
26 |  *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
27 |  *       <attribute name="courseId" use="required" type="{http://www.w3.org/2001/XMLSchema}string" />
28 |  *     </restriction>
29 |  *   </complexContent>
30 |  * </complexType>
31 |  * 
32 | * 33 | * 34 | */ 35 | @XmlAccessorType(XmlAccessType.FIELD) 36 | @XmlType(name = "") 37 | @XmlRootElement(name = "getCourseRequest") 38 | public class GetCourseRequest { 39 | 40 | @XmlAttribute(name = "courseId", required = true) 41 | protected String courseId; 42 | 43 | /** 44 | * Gets the value of the courseId property. 45 | * 46 | * @return 47 | * possible object is 48 | * {@link String } 49 | * 50 | */ 51 | public String getCourseId() { 52 | return courseId; 53 | } 54 | 55 | /** 56 | * Sets the value of the courseId property. 57 | * 58 | * @param value 59 | * allowed object is 60 | * {@link String } 61 | * 62 | */ 63 | public void setCourseId(String value) { 64 | this.courseId = value; 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /generic/int-v4-full/src/main/java/xpadro/spring/integration/ws/types/package-info.java: -------------------------------------------------------------------------------- 1 | // 2 | // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.4-2 3 | // See http://java.sun.com/xml/jaxb 4 | // Any modifications to this file will be lost upon recompilation of the source schema. 5 | // Generated on: 2014.04.24 at 04:39:37 PM CEST 6 | // 7 | 8 | @javax.xml.bind.annotation.XmlSchema(namespace = "http://www.xpadro.spring.samples.com/courses", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED) 9 | package xpadro.spring.integration.ws.types; 10 | -------------------------------------------------------------------------------- /generic/int-v4-full/src/test/java/xpadro/spring/integration/test/TestApp.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.test; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | import static org.junit.Assert.assertNotNull; 5 | 6 | import org.junit.Test; 7 | import org.junit.runner.RunWith; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.test.context.ContextConfiguration; 10 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 11 | 12 | import xpadro.spring.integration.configuration.InfrastructureConfiguration; 13 | import xpadro.spring.integration.gateway.CourseService; 14 | 15 | @RunWith(SpringJUnit4ClassRunner.class) 16 | @ContextConfiguration(classes={InfrastructureConfiguration.class}) 17 | public class TestApp { 18 | @Autowired 19 | CourseService service; 20 | 21 | @Test 22 | public void testFlow() { 23 | String courseName = service.findCourse("BC-45"); 24 | assertNotNull(courseName); 25 | assertEquals("Introduction to Java", courseName); 26 | 27 | courseName = service.findCourse("DF-21"); 28 | assertNotNull(courseName); 29 | assertEquals("Functional Programming Principles in Scala", courseName); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /http/README.md: -------------------------------------------------------------------------------- 1 | # HTTP 2 | Spring Integration HTTP support 3 | #### Version 4 4 | Examples using Spring Integration 4 release: 5 | 6 | * [http-xml] - An example of a web application that exposes a RESTful API using HTTP inbound channel adapters. The configuration of this application is implemented with XML. 7 | * Blog post: http://xpadro.com/2014/12/exposing-http-restful-api-with-inbound.html 8 | 9 | * [http-dsl7] - An example of a web application that exposes a RESTful API using HTTP inbound channel adapters. The configuration of this application is fully implemented with JavaConfig and Spring Integration Java DSL. This example is implemented with a Java version prior to Java 8. Hence, it does not use lambdas. To check the Java 8 version example please select the int-http-dsl project. 10 | 11 | * [http-dsl] - An example of a web application that exposes a RESTful API using HTTP inbound channel adapters. The configuration of this application is fully implemented with JavaConfig and Spring Integration Java DSL. This example is implemented with a Java 8 version, making use of lambdas. 12 | * Blog post: http://xpadro.com/2014/12/exposing-http-restful-api-with-inbound_22.html 13 | 14 | 15 | 16 | [http-xml]: https://github.com/xpadro/spring-integration/tree/master/http/http-xml 17 | [http-dsl7]: https://github.com/xpadro/spring-integration/tree/master/http/http-dsl7 18 | [http-dsl]: https://github.com/xpadro/spring-integration/tree/master/http/http-dsl 19 | 20 | -------------------------------------------------------------------------------- /http/http-dsl/src/main/java/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /http/http-dsl/src/main/java/xpadro/spring/integration/server/handler/PersonEndpoint.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.server.handler; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.http.HttpStatus; 5 | import org.springframework.integration.support.MessageBuilder; 6 | import org.springframework.messaging.Message; 7 | import org.springframework.stereotype.Component; 8 | 9 | import xpadro.spring.integration.server.model.ServerPerson; 10 | import xpadro.spring.integration.server.service.PersonService; 11 | 12 | @Component 13 | public class PersonEndpoint { 14 | private static final String STATUSCODE_HEADER = "http_statusCode"; 15 | 16 | @Autowired 17 | private PersonService service; 18 | 19 | public Message get(Message msg) { 20 | long id = Long.valueOf(msg.getPayload()); 21 | ServerPerson person = service.getPerson(id); 22 | 23 | if (person == null) { 24 | return MessageBuilder.fromMessage(msg) 25 | .copyHeadersIfAbsent(msg.getHeaders()) 26 | .setHeader(STATUSCODE_HEADER, HttpStatus.NOT_FOUND) 27 | .build(); 28 | } 29 | 30 | return MessageBuilder.withPayload(person) 31 | .copyHeadersIfAbsent(msg.getHeaders()) 32 | .setHeader(STATUSCODE_HEADER, HttpStatus.OK) 33 | .build(); 34 | } 35 | 36 | public void put(Message msg) { 37 | service.updatePerson(msg.getPayload()); 38 | } 39 | 40 | public void post(Message msg) { 41 | service.insertPerson(msg.getPayload()); 42 | } 43 | 44 | public void delete(Message msg) { 45 | long id = Long.valueOf(msg.getPayload()); 46 | service.deletePerson(id); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /http/http-dsl/src/main/java/xpadro/spring/integration/server/model/ServerPerson.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.server.model; 2 | 3 | import java.io.Serializable; 4 | 5 | public class ServerPerson implements Serializable { 6 | private static final long serialVersionUID = 1L; 7 | 8 | private long id; 9 | private String name; 10 | private Integer age; 11 | 12 | public ServerPerson() {} 13 | 14 | public ServerPerson(long id, String name, Integer age) { 15 | this.id = id; 16 | this.name = name; 17 | this.age = age; 18 | } 19 | 20 | public long getId() { 21 | return this.id; 22 | } 23 | 24 | public void setId(long id) { 25 | this.id = id; 26 | } 27 | 28 | public String getName() { 29 | return this.name; 30 | } 31 | 32 | public void setName(String name) { 33 | this.name = name; 34 | } 35 | 36 | public Integer getAge() { 37 | return age; 38 | } 39 | 40 | public void setAge(Integer age) { 41 | this.age = age; 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /http/http-dsl/src/main/java/xpadro/spring/integration/server/service/PersonService.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.server.service; 2 | 3 | import xpadro.spring.integration.server.model.ServerPerson; 4 | 5 | public interface PersonService { 6 | 7 | ServerPerson getPerson(long id); 8 | 9 | void updatePerson(ServerPerson person); 10 | 11 | void insertPerson(ServerPerson person); 12 | 13 | void deletePerson(long id); 14 | } 15 | -------------------------------------------------------------------------------- /http/http-dsl/src/main/java/xpadro/spring/integration/server/service/PersonServiceImpl.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.server.service; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | import org.slf4j.Logger; 7 | import org.slf4j.LoggerFactory; 8 | import org.springframework.stereotype.Service; 9 | 10 | import xpadro.spring.integration.server.model.ServerPerson; 11 | 12 | @Service 13 | public class PersonServiceImpl implements PersonService { 14 | private final Logger logger = LoggerFactory.getLogger(getClass()); 15 | private final Map persons = new HashMap(); 16 | 17 | public PersonServiceImpl() { 18 | persons.put(1l, new ServerPerson(1, "John", 25)); 19 | persons.put(2l, new ServerPerson(2, "Steve", 19)); 20 | persons.put(3l, new ServerPerson(3, "Mike", 38)); 21 | persons.put(4l, new ServerPerson(4, "Julia", 41)); 22 | } 23 | 24 | @Override 25 | public ServerPerson getPerson(long id) { 26 | logger.info("retrieving person with id {}", id); 27 | return persons.get(id); 28 | } 29 | 30 | @Override 31 | public void updatePerson(ServerPerson person) { 32 | if (persons.get(person.getId()) != null) { 33 | logger.info("updating person with id {}", person.getId()); 34 | persons.put(person.getId(), person); 35 | } 36 | } 37 | 38 | @Override 39 | public void insertPerson(ServerPerson person) { 40 | if (persons.get(person.getId()) == null) { 41 | logger.info("inserting new person with id {}", person.getId()); 42 | persons.put(person.getId(), person); 43 | } 44 | } 45 | 46 | @Override 47 | public void deletePerson(long id) { 48 | logger.info("deleting person with id {}", id); 49 | persons.remove(id); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /http/http-dsl/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | 7 | springServlet 8 | org.springframework.web.servlet.DispatcherServlet 9 | 10 | contextClass 11 | org.springframework.web.context.support.AnnotationConfigWebApplicationContext 12 | 13 | 14 | contextConfigLocation 15 | xpadro.spring.integration.server.configuration 16 | 17 | 18 | 19 | springServlet 20 | /spring/* 21 | 22 | -------------------------------------------------------------------------------- /http/http-dsl/src/test/java/xpadro/spring/integration/test/DeleteOperationsTest.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.test; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import java.util.Arrays; 6 | 7 | import org.junit.Assert; 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | import org.junit.runners.BlockJUnit4ClassRunner; 11 | import org.springframework.http.HttpEntity; 12 | import org.springframework.http.HttpHeaders; 13 | import org.springframework.http.HttpMethod; 14 | import org.springframework.http.HttpStatus; 15 | import org.springframework.http.MediaType; 16 | import org.springframework.http.ResponseEntity; 17 | import org.springframework.web.client.HttpClientErrorException; 18 | import org.springframework.web.client.RestTemplate; 19 | 20 | import xpadro.spring.integration.test.model.ClientPerson; 21 | 22 | @RunWith(BlockJUnit4ClassRunner.class) 23 | public class DeleteOperationsTest { 24 | private static final String URL = "http://localhost:8080/spring/persons/{personId}"; 25 | private final RestTemplate restTemplate = new RestTemplate(); 26 | 27 | private HttpHeaders buildHeaders() { 28 | HttpHeaders headers = new HttpHeaders(); 29 | headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); 30 | headers.setContentType(MediaType.APPLICATION_JSON); 31 | 32 | return headers; 33 | } 34 | 35 | @Test 36 | public void deleteResource_noContentStatusCodeReturned() { 37 | HttpEntity entity = new HttpEntity<>(buildHeaders()); 38 | ResponseEntity response = restTemplate.exchange(URL, HttpMethod.DELETE, entity, ClientPerson.class, 3); 39 | assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode()); 40 | 41 | try { 42 | response = restTemplate.exchange(URL, HttpMethod.GET, entity, ClientPerson.class, 3); 43 | Assert.fail("404 error expected"); 44 | } catch (HttpClientErrorException e) { 45 | assertEquals(HttpStatus.NOT_FOUND, e.getStatusCode()); 46 | } 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /http/http-dsl/src/test/java/xpadro/spring/integration/test/PostOperationsTest.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.test; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import java.util.Arrays; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | import org.junit.runners.BlockJUnit4ClassRunner; 10 | import org.springframework.http.HttpEntity; 11 | import org.springframework.http.HttpHeaders; 12 | import org.springframework.http.HttpMethod; 13 | import org.springframework.http.HttpStatus; 14 | import org.springframework.http.MediaType; 15 | import org.springframework.http.ResponseEntity; 16 | import org.springframework.web.client.RestTemplate; 17 | 18 | import xpadro.spring.integration.test.model.ClientPerson; 19 | 20 | @RunWith(BlockJUnit4ClassRunner.class) 21 | public class PostOperationsTest { 22 | private static final String POST_URL = "http://localhost:8080/spring/persons"; 23 | private static final String GET_URL = "http://localhost:8080/spring/persons/{personId}"; 24 | private final RestTemplate restTemplate = new RestTemplate(); 25 | 26 | private HttpHeaders buildHeaders() { 27 | HttpHeaders headers = new HttpHeaders(); 28 | headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); 29 | headers.setContentType(MediaType.APPLICATION_JSON); 30 | 31 | return headers; 32 | } 33 | 34 | @Test 35 | public void addResource_noContentStatusCodeReturned() { 36 | ClientPerson person = new ClientPerson(9, "Jana"); 37 | HttpEntity entity = new HttpEntity(person, buildHeaders()); 38 | 39 | ResponseEntity response = restTemplate.exchange(POST_URL, HttpMethod.POST, entity, ClientPerson.class); 40 | assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode()); 41 | 42 | HttpEntity getEntity = new HttpEntity<>(buildHeaders()); 43 | response = restTemplate.exchange(GET_URL, HttpMethod.GET, getEntity, ClientPerson.class, 9); 44 | person = response.getBody(); 45 | assertEquals("Jana", person.getName()); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /http/http-dsl/src/test/java/xpadro/spring/integration/test/PutOperationsTest.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.test; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import java.util.Arrays; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | import org.junit.runners.BlockJUnit4ClassRunner; 10 | import org.springframework.http.HttpEntity; 11 | import org.springframework.http.HttpHeaders; 12 | import org.springframework.http.HttpMethod; 13 | import org.springframework.http.HttpStatus; 14 | import org.springframework.http.MediaType; 15 | import org.springframework.http.ResponseEntity; 16 | import org.springframework.web.client.RestTemplate; 17 | 18 | import xpadro.spring.integration.test.model.ClientPerson; 19 | 20 | @RunWith(BlockJUnit4ClassRunner.class) 21 | public class PutOperationsTest { 22 | private static final String URL = "http://localhost:8080/spring/persons/{personId}"; 23 | private final RestTemplate restTemplate = new RestTemplate(); 24 | 25 | private HttpHeaders buildHeaders() { 26 | HttpHeaders headers = new HttpHeaders(); 27 | headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); 28 | headers.setContentType(MediaType.APPLICATION_JSON); 29 | 30 | return headers; 31 | } 32 | 33 | @Test 34 | public void updateResource_noContentStatusCodeReturned() { 35 | HttpEntity getEntity = new HttpEntity<>(buildHeaders()); 36 | ResponseEntity response = restTemplate.exchange(URL, HttpMethod.GET, getEntity, ClientPerson.class, 4); 37 | ClientPerson person = response.getBody(); 38 | person.setName("Sandra"); 39 | HttpEntity putEntity = new HttpEntity(person, buildHeaders()); 40 | 41 | response = restTemplate.exchange(URL, HttpMethod.PUT, putEntity, ClientPerson.class, 4); 42 | assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode()); 43 | 44 | response = restTemplate.exchange(URL, HttpMethod.GET, getEntity, ClientPerson.class, 4); 45 | person = response.getBody(); 46 | assertEquals("Sandra", person.getName()); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /http/http-dsl/src/test/java/xpadro/spring/integration/test/model/ClientPerson.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.test.model; 2 | 3 | import java.io.Serializable; 4 | 5 | import com.fasterxml.jackson.annotation.JsonIgnoreProperties; 6 | import com.fasterxml.jackson.annotation.JsonProperty; 7 | 8 | @JsonIgnoreProperties(ignoreUnknown = true) 9 | public class ClientPerson implements Serializable { 10 | private static final long serialVersionUID = 1L; 11 | 12 | @JsonProperty("id") 13 | private int myId; 14 | private String name; 15 | 16 | public ClientPerson() {} 17 | 18 | public ClientPerson(int id, String name) { 19 | this.myId = id; 20 | this.name = name; 21 | } 22 | 23 | public void setMyId(int id) { 24 | this.myId = id; 25 | } 26 | 27 | public void setName(String name) { 28 | this.name = name; 29 | } 30 | 31 | public int getMyId() { 32 | return this.myId; 33 | } 34 | 35 | public String getName() { 36 | return this.name; 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /http/http-dsl7/src/main/java/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /http/http-dsl7/src/main/java/xpadro/spring/integration/server/handler/PersonEndpoint.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.server.handler; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.http.HttpStatus; 5 | import org.springframework.integration.support.MessageBuilder; 6 | import org.springframework.messaging.Message; 7 | import org.springframework.stereotype.Component; 8 | 9 | import xpadro.spring.integration.server.model.ServerPerson; 10 | import xpadro.spring.integration.server.service.PersonService; 11 | 12 | @Component 13 | public class PersonEndpoint { 14 | private static final String STATUSCODE_HEADER = "http_statusCode"; 15 | 16 | @Autowired 17 | private PersonService service; 18 | 19 | public Message get(Message msg) { 20 | long id = Long.valueOf(msg.getPayload()); 21 | ServerPerson person = service.getPerson(id); 22 | 23 | if (person == null) { 24 | return MessageBuilder.fromMessage(msg) 25 | .copyHeadersIfAbsent(msg.getHeaders()) 26 | .setHeader(STATUSCODE_HEADER, HttpStatus.NOT_FOUND) 27 | .build(); 28 | } 29 | 30 | return MessageBuilder.withPayload(person) 31 | .copyHeadersIfAbsent(msg.getHeaders()) 32 | .setHeader(STATUSCODE_HEADER, HttpStatus.OK) 33 | .build(); 34 | } 35 | 36 | public void put(Message msg) { 37 | service.updatePerson(msg.getPayload()); 38 | } 39 | 40 | public void post(Message msg) { 41 | service.insertPerson(msg.getPayload()); 42 | } 43 | 44 | public void delete(Message msg) { 45 | long id = Long.valueOf(msg.getPayload()); 46 | service.deletePerson(id); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /http/http-dsl7/src/main/java/xpadro/spring/integration/server/model/ServerPerson.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.server.model; 2 | 3 | import java.io.Serializable; 4 | 5 | public class ServerPerson implements Serializable { 6 | private static final long serialVersionUID = 1L; 7 | 8 | private long id; 9 | private String name; 10 | private Integer age; 11 | 12 | public ServerPerson() {} 13 | 14 | public ServerPerson(long id, String name, Integer age) { 15 | this.id = id; 16 | this.name = name; 17 | this.age = age; 18 | } 19 | 20 | public long getId() { 21 | return this.id; 22 | } 23 | 24 | public void setId(long id) { 25 | this.id = id; 26 | } 27 | 28 | public String getName() { 29 | return this.name; 30 | } 31 | 32 | public void setName(String name) { 33 | this.name = name; 34 | } 35 | 36 | public Integer getAge() { 37 | return age; 38 | } 39 | 40 | public void setAge(Integer age) { 41 | this.age = age; 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /http/http-dsl7/src/main/java/xpadro/spring/integration/server/service/PersonService.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.server.service; 2 | 3 | import xpadro.spring.integration.server.model.ServerPerson; 4 | 5 | public interface PersonService { 6 | 7 | ServerPerson getPerson(long id); 8 | 9 | void updatePerson(ServerPerson person); 10 | 11 | void insertPerson(ServerPerson person); 12 | 13 | void deletePerson(long id); 14 | } 15 | -------------------------------------------------------------------------------- /http/http-dsl7/src/main/java/xpadro/spring/integration/server/service/PersonServiceImpl.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.server.service; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | import org.slf4j.Logger; 7 | import org.slf4j.LoggerFactory; 8 | import org.springframework.stereotype.Service; 9 | 10 | import xpadro.spring.integration.server.model.ServerPerson; 11 | 12 | @Service 13 | public class PersonServiceImpl implements PersonService { 14 | private final Logger logger = LoggerFactory.getLogger(getClass()); 15 | private final Map persons = new HashMap(); 16 | 17 | public PersonServiceImpl() { 18 | persons.put(1l, new ServerPerson(1, "John", 25)); 19 | persons.put(2l, new ServerPerson(2, "Steve", 19)); 20 | persons.put(3l, new ServerPerson(3, "Mike", 38)); 21 | persons.put(4l, new ServerPerson(4, "Julia", 41)); 22 | } 23 | 24 | @Override 25 | public ServerPerson getPerson(long id) { 26 | logger.info("retrieving person with id {}", id); 27 | return persons.get(id); 28 | } 29 | 30 | @Override 31 | public void updatePerson(ServerPerson person) { 32 | if (persons.get(person.getId()) != null) { 33 | logger.info("updating person with id {}", person.getId()); 34 | persons.put(person.getId(), person); 35 | } 36 | } 37 | 38 | @Override 39 | public void insertPerson(ServerPerson person) { 40 | if (persons.get(person.getId()) == null) { 41 | logger.info("inserting new person with id {}", person.getId()); 42 | persons.put(person.getId(), person); 43 | } 44 | } 45 | 46 | @Override 47 | public void deletePerson(long id) { 48 | logger.info("deleting person with id {}", id); 49 | persons.remove(id); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /http/http-dsl7/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | 7 | springServlet 8 | org.springframework.web.servlet.DispatcherServlet 9 | 10 | contextClass 11 | org.springframework.web.context.support.AnnotationConfigWebApplicationContext 12 | 13 | 14 | contextConfigLocation 15 | xpadro.spring.integration.server.configuration 16 | 17 | 18 | 19 | springServlet 20 | /spring/* 21 | 22 | -------------------------------------------------------------------------------- /http/http-dsl7/src/test/java/xpadro/spring/integration/test/DeleteOperationsTest.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.test; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import java.util.Arrays; 6 | 7 | import org.junit.Assert; 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | import org.junit.runners.BlockJUnit4ClassRunner; 11 | import org.springframework.http.HttpEntity; 12 | import org.springframework.http.HttpHeaders; 13 | import org.springframework.http.HttpMethod; 14 | import org.springframework.http.HttpStatus; 15 | import org.springframework.http.MediaType; 16 | import org.springframework.http.ResponseEntity; 17 | import org.springframework.web.client.HttpClientErrorException; 18 | import org.springframework.web.client.RestTemplate; 19 | 20 | import xpadro.spring.integration.test.model.ClientPerson; 21 | 22 | @RunWith(BlockJUnit4ClassRunner.class) 23 | public class DeleteOperationsTest { 24 | private static final String URL = "http://localhost:8080/spring/persons/{personId}"; 25 | private final RestTemplate restTemplate = new RestTemplate(); 26 | 27 | private HttpHeaders buildHeaders() { 28 | HttpHeaders headers = new HttpHeaders(); 29 | headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); 30 | headers.setContentType(MediaType.APPLICATION_JSON); 31 | 32 | return headers; 33 | } 34 | 35 | @Test 36 | public void deleteResource_noContentStatusCodeReturned() { 37 | HttpEntity entity = new HttpEntity<>(buildHeaders()); 38 | ResponseEntity response = restTemplate.exchange(URL, HttpMethod.DELETE, entity, ClientPerson.class, 3); 39 | assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode()); 40 | 41 | try { 42 | response = restTemplate.exchange(URL, HttpMethod.GET, entity, ClientPerson.class, 3); 43 | Assert.fail("404 error expected"); 44 | } catch (HttpClientErrorException e) { 45 | assertEquals(HttpStatus.NOT_FOUND, e.getStatusCode()); 46 | } 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /http/http-dsl7/src/test/java/xpadro/spring/integration/test/PostOperationsTest.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.test; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import java.util.Arrays; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | import org.junit.runners.BlockJUnit4ClassRunner; 10 | import org.springframework.http.HttpEntity; 11 | import org.springframework.http.HttpHeaders; 12 | import org.springframework.http.HttpMethod; 13 | import org.springframework.http.HttpStatus; 14 | import org.springframework.http.MediaType; 15 | import org.springframework.http.ResponseEntity; 16 | import org.springframework.web.client.RestTemplate; 17 | 18 | import xpadro.spring.integration.test.model.ClientPerson; 19 | 20 | @RunWith(BlockJUnit4ClassRunner.class) 21 | public class PostOperationsTest { 22 | private static final String POST_URL = "http://localhost:8080/spring/persons"; 23 | private static final String GET_URL = "http://localhost:8080/spring/persons/{personId}"; 24 | private final RestTemplate restTemplate = new RestTemplate(); 25 | 26 | private HttpHeaders buildHeaders() { 27 | HttpHeaders headers = new HttpHeaders(); 28 | headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); 29 | headers.setContentType(MediaType.APPLICATION_JSON); 30 | 31 | return headers; 32 | } 33 | 34 | @Test 35 | public void addResource_noContentStatusCodeReturned() { 36 | ClientPerson person = new ClientPerson(9, "Jana"); 37 | HttpEntity entity = new HttpEntity(person, buildHeaders()); 38 | 39 | ResponseEntity response = restTemplate.exchange(POST_URL, HttpMethod.POST, entity, ClientPerson.class); 40 | assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode()); 41 | 42 | HttpEntity getEntity = new HttpEntity<>(buildHeaders()); 43 | response = restTemplate.exchange(GET_URL, HttpMethod.GET, getEntity, ClientPerson.class, 9); 44 | person = response.getBody(); 45 | assertEquals("Jana", person.getName()); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /http/http-dsl7/src/test/java/xpadro/spring/integration/test/PutOperationsTest.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.test; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import java.util.Arrays; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | import org.junit.runners.BlockJUnit4ClassRunner; 10 | import org.springframework.http.HttpEntity; 11 | import org.springframework.http.HttpHeaders; 12 | import org.springframework.http.HttpMethod; 13 | import org.springframework.http.HttpStatus; 14 | import org.springframework.http.MediaType; 15 | import org.springframework.http.ResponseEntity; 16 | import org.springframework.web.client.RestTemplate; 17 | 18 | import xpadro.spring.integration.test.model.ClientPerson; 19 | 20 | @RunWith(BlockJUnit4ClassRunner.class) 21 | public class PutOperationsTest { 22 | private static final String URL = "http://localhost:8080/spring/persons/{personId}"; 23 | private final RestTemplate restTemplate = new RestTemplate(); 24 | 25 | private HttpHeaders buildHeaders() { 26 | HttpHeaders headers = new HttpHeaders(); 27 | headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); 28 | headers.setContentType(MediaType.APPLICATION_JSON); 29 | 30 | return headers; 31 | } 32 | 33 | @Test 34 | public void updateResource_noContentStatusCodeReturned() { 35 | HttpEntity getEntity = new HttpEntity<>(buildHeaders()); 36 | ResponseEntity response = restTemplate.exchange(URL, HttpMethod.GET, getEntity, ClientPerson.class, 4); 37 | ClientPerson person = response.getBody(); 38 | person.setName("Sandra"); 39 | HttpEntity putEntity = new HttpEntity(person, buildHeaders()); 40 | 41 | response = restTemplate.exchange(URL, HttpMethod.PUT, putEntity, ClientPerson.class, 4); 42 | assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode()); 43 | 44 | response = restTemplate.exchange(URL, HttpMethod.GET, getEntity, ClientPerson.class, 4); 45 | person = response.getBody(); 46 | assertEquals("Sandra", person.getName()); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /http/http-dsl7/src/test/java/xpadro/spring/integration/test/model/ClientPerson.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.test.model; 2 | 3 | import java.io.Serializable; 4 | 5 | import com.fasterxml.jackson.annotation.JsonIgnoreProperties; 6 | import com.fasterxml.jackson.annotation.JsonProperty; 7 | 8 | @JsonIgnoreProperties(ignoreUnknown = true) 9 | public class ClientPerson implements Serializable { 10 | private static final long serialVersionUID = 1L; 11 | 12 | @JsonProperty("id") 13 | private int myId; 14 | private String name; 15 | 16 | public ClientPerson() {} 17 | 18 | public ClientPerson(int id, String name) { 19 | this.myId = id; 20 | this.name = name; 21 | } 22 | 23 | public void setMyId(int id) { 24 | this.myId = id; 25 | } 26 | 27 | public void setName(String name) { 28 | this.name = name; 29 | } 30 | 31 | public int getMyId() { 32 | return this.myId; 33 | } 34 | 35 | public String getName() { 36 | return this.name; 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /http/http-xml/src/main/java/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /http/http-xml/src/main/java/xpadro/spring/integration/server/handler/PersonEndpoint.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.server.handler; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.http.HttpStatus; 5 | import org.springframework.integration.support.MessageBuilder; 6 | import org.springframework.messaging.Message; 7 | import org.springframework.stereotype.Component; 8 | 9 | import xpadro.spring.integration.server.model.ServerPerson; 10 | import xpadro.spring.integration.server.service.PersonService; 11 | 12 | @Component 13 | public class PersonEndpoint { 14 | private static final String STATUSCODE_HEADER = "http_statusCode"; 15 | 16 | @Autowired 17 | private PersonService service; 18 | 19 | public Message get(Message msg) { 20 | long id = Long.valueOf(msg.getPayload()); 21 | ServerPerson person = service.getPerson(id); 22 | 23 | if (person == null) { 24 | return MessageBuilder.fromMessage(msg) 25 | .copyHeadersIfAbsent(msg.getHeaders()) 26 | .setHeader(STATUSCODE_HEADER, HttpStatus.NOT_FOUND) 27 | .build(); 28 | } 29 | 30 | return MessageBuilder.withPayload(person) 31 | .copyHeadersIfAbsent(msg.getHeaders()) 32 | .setHeader(STATUSCODE_HEADER, HttpStatus.OK) 33 | .build(); 34 | } 35 | 36 | public void put(Message msg) { 37 | service.updatePerson(msg.getPayload()); 38 | } 39 | 40 | public void post(Message msg) { 41 | service.insertPerson(msg.getPayload()); 42 | } 43 | 44 | public void delete(Message msg) { 45 | long id = Long.valueOf(msg.getPayload()); 46 | service.deletePerson(id); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /http/http-xml/src/main/java/xpadro/spring/integration/server/model/ServerPerson.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.server.model; 2 | 3 | import java.io.Serializable; 4 | 5 | public class ServerPerson implements Serializable { 6 | private static final long serialVersionUID = 1L; 7 | 8 | private long id; 9 | private String name; 10 | private Integer age; 11 | 12 | public ServerPerson() {} 13 | 14 | public ServerPerson(long id, String name, Integer age) { 15 | this.id = id; 16 | this.name = name; 17 | this.age = age; 18 | } 19 | 20 | public long getId() { 21 | return this.id; 22 | } 23 | 24 | public void setId(long id) { 25 | this.id = id; 26 | } 27 | 28 | public String getName() { 29 | return this.name; 30 | } 31 | 32 | public void setName(String name) { 33 | this.name = name; 34 | } 35 | 36 | public Integer getAge() { 37 | return age; 38 | } 39 | 40 | public void setAge(Integer age) { 41 | this.age = age; 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /http/http-xml/src/main/java/xpadro/spring/integration/server/service/PersonService.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.server.service; 2 | 3 | import xpadro.spring.integration.server.model.ServerPerson; 4 | 5 | public interface PersonService { 6 | 7 | ServerPerson getPerson(long id); 8 | 9 | void updatePerson(ServerPerson person); 10 | 11 | void insertPerson(ServerPerson person); 12 | 13 | void deletePerson(long id); 14 | } 15 | -------------------------------------------------------------------------------- /http/http-xml/src/main/java/xpadro/spring/integration/server/service/PersonServiceImpl.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.server.service; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | import org.slf4j.Logger; 7 | import org.slf4j.LoggerFactory; 8 | import org.springframework.stereotype.Service; 9 | 10 | import xpadro.spring.integration.server.model.ServerPerson; 11 | 12 | @Service 13 | public class PersonServiceImpl implements PersonService { 14 | private final Logger logger = LoggerFactory.getLogger(getClass()); 15 | private final Map persons = new HashMap(); 16 | 17 | public PersonServiceImpl() { 18 | persons.put(1l, new ServerPerson(1, "John", 25)); 19 | persons.put(2l, new ServerPerson(2, "Steve", 19)); 20 | persons.put(3l, new ServerPerson(3, "Mike", 38)); 21 | persons.put(4l, new ServerPerson(4, "Julia", 41)); 22 | } 23 | 24 | @Override 25 | public ServerPerson getPerson(long id) { 26 | logger.info("retrieving person with id {}", id); 27 | return persons.get(id); 28 | } 29 | 30 | @Override 31 | public void updatePerson(ServerPerson person) { 32 | if (persons.get(person.getId()) != null) { 33 | logger.info("updating person with id {}", person.getId()); 34 | persons.put(person.getId(), person); 35 | } 36 | } 37 | 38 | @Override 39 | public void insertPerson(ServerPerson person) { 40 | if (persons.get(person.getId()) == null) { 41 | logger.info("inserting new person with id {}", person.getId()); 42 | persons.put(person.getId(), person); 43 | } 44 | } 45 | 46 | @Override 47 | public void deletePerson(long id) { 48 | logger.info("deleting person with id {}", id); 49 | persons.remove(id); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /http/http-xml/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | 7 | springServlet 8 | org.springframework.web.servlet.DispatcherServlet 9 | 10 | contextConfigLocation 11 | classpath:xpadro/spring/integration/configuration/http-inbound-config.xml 12 | 13 | 14 | 15 | springServlet 16 | /spring/* 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /http/http-xml/src/test/java/xpadro/spring/integration/test/DeleteOperationsTest.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.test; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import java.util.Arrays; 6 | 7 | import org.junit.Assert; 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | import org.junit.runners.BlockJUnit4ClassRunner; 11 | import org.springframework.http.HttpEntity; 12 | import org.springframework.http.HttpHeaders; 13 | import org.springframework.http.HttpMethod; 14 | import org.springframework.http.HttpStatus; 15 | import org.springframework.http.MediaType; 16 | import org.springframework.http.ResponseEntity; 17 | import org.springframework.web.client.HttpClientErrorException; 18 | import org.springframework.web.client.RestTemplate; 19 | 20 | import xpadro.spring.integration.test.model.ClientPerson; 21 | 22 | @RunWith(BlockJUnit4ClassRunner.class) 23 | public class DeleteOperationsTest { 24 | private static final String URL = "http://localhost:8080/spring/persons/{personId}"; 25 | private final RestTemplate restTemplate = new RestTemplate(); 26 | 27 | private HttpHeaders buildHeaders() { 28 | HttpHeaders headers = new HttpHeaders(); 29 | headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); 30 | headers.setContentType(MediaType.APPLICATION_JSON); 31 | 32 | return headers; 33 | } 34 | 35 | @Test 36 | public void deleteResource_noContentStatusCodeReturned() { 37 | HttpEntity entity = new HttpEntity<>(buildHeaders()); 38 | ResponseEntity response = restTemplate.exchange(URL, HttpMethod.DELETE, entity, ClientPerson.class, 3); 39 | assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode()); 40 | 41 | try { 42 | response = restTemplate.exchange(URL, HttpMethod.GET, entity, ClientPerson.class, 3); 43 | Assert.fail("404 error expected"); 44 | } catch (HttpClientErrorException e) { 45 | assertEquals(HttpStatus.NOT_FOUND, e.getStatusCode()); 46 | } 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /http/http-xml/src/test/java/xpadro/spring/integration/test/PostOperationsTest.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.test; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import java.util.Arrays; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | import org.junit.runners.BlockJUnit4ClassRunner; 10 | import org.springframework.http.HttpEntity; 11 | import org.springframework.http.HttpHeaders; 12 | import org.springframework.http.HttpMethod; 13 | import org.springframework.http.HttpStatus; 14 | import org.springframework.http.MediaType; 15 | import org.springframework.http.ResponseEntity; 16 | import org.springframework.web.client.RestTemplate; 17 | 18 | import xpadro.spring.integration.test.model.ClientPerson; 19 | 20 | @RunWith(BlockJUnit4ClassRunner.class) 21 | public class PostOperationsTest { 22 | private static final String POST_URL = "http://localhost:8080/spring/persons"; 23 | private static final String GET_URL = "http://localhost:8080/spring/persons/{personId}"; 24 | private final RestTemplate restTemplate = new RestTemplate(); 25 | 26 | private HttpHeaders buildHeaders() { 27 | HttpHeaders headers = new HttpHeaders(); 28 | headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); 29 | headers.setContentType(MediaType.APPLICATION_JSON); 30 | 31 | return headers; 32 | } 33 | 34 | @Test 35 | public void addResource_noContentStatusCodeReturned() { 36 | ClientPerson person = new ClientPerson(9, "Jana"); 37 | HttpEntity entity = new HttpEntity(person, buildHeaders()); 38 | 39 | ResponseEntity response = restTemplate.exchange(POST_URL, HttpMethod.POST, entity, ClientPerson.class); 40 | assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode()); 41 | 42 | HttpEntity getEntity = new HttpEntity<>(buildHeaders()); 43 | response = restTemplate.exchange(GET_URL, HttpMethod.GET, getEntity, ClientPerson.class, 9); 44 | person = response.getBody(); 45 | assertEquals("Jana", person.getName()); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /http/http-xml/src/test/java/xpadro/spring/integration/test/PutOperationsTest.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.test; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import java.util.Arrays; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | import org.junit.runners.BlockJUnit4ClassRunner; 10 | import org.springframework.http.HttpEntity; 11 | import org.springframework.http.HttpHeaders; 12 | import org.springframework.http.HttpMethod; 13 | import org.springframework.http.HttpStatus; 14 | import org.springframework.http.MediaType; 15 | import org.springframework.http.ResponseEntity; 16 | import org.springframework.web.client.RestTemplate; 17 | 18 | import xpadro.spring.integration.test.model.ClientPerson; 19 | 20 | @RunWith(BlockJUnit4ClassRunner.class) 21 | public class PutOperationsTest { 22 | private static final String URL = "http://localhost:8080/spring/persons/{personId}"; 23 | private final RestTemplate restTemplate = new RestTemplate(); 24 | 25 | private HttpHeaders buildHeaders() { 26 | HttpHeaders headers = new HttpHeaders(); 27 | headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); 28 | headers.setContentType(MediaType.APPLICATION_JSON); 29 | 30 | return headers; 31 | } 32 | 33 | @Test 34 | public void updateResource_noContentStatusCodeReturned() { 35 | HttpEntity getEntity = new HttpEntity<>(buildHeaders()); 36 | ResponseEntity response = restTemplate.exchange(URL, HttpMethod.GET, getEntity, ClientPerson.class, 4); 37 | ClientPerson person = response.getBody(); 38 | person.setName("Sandra"); 39 | HttpEntity putEntity = new HttpEntity(person, buildHeaders()); 40 | 41 | response = restTemplate.exchange(URL, HttpMethod.PUT, putEntity, ClientPerson.class, 4); 42 | assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode()); 43 | 44 | response = restTemplate.exchange(URL, HttpMethod.GET, getEntity, ClientPerson.class, 4); 45 | person = response.getBody(); 46 | assertEquals("Sandra", person.getName()); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /http/http-xml/src/test/java/xpadro/spring/integration/test/model/ClientPerson.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.test.model; 2 | 3 | import java.io.Serializable; 4 | 5 | import com.fasterxml.jackson.annotation.JsonIgnoreProperties; 6 | import com.fasterxml.jackson.annotation.JsonProperty; 7 | 8 | @JsonIgnoreProperties(ignoreUnknown = true) 9 | public class ClientPerson implements Serializable { 10 | private static final long serialVersionUID = 1L; 11 | 12 | @JsonProperty("id") 13 | private int myId; 14 | private String name; 15 | 16 | public ClientPerson() {} 17 | 18 | public ClientPerson(int id, String name) { 19 | this.myId = id; 20 | this.name = name; 21 | } 22 | 23 | public void setMyId(int id) { 24 | this.myId = id; 25 | } 26 | 27 | public void setName(String name) { 28 | this.name = name; 29 | } 30 | 31 | public int getMyId() { 32 | return this.myId; 33 | } 34 | 35 | public String getName() { 36 | return this.name; 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /mongodb/README.md: -------------------------------------------------------------------------------- 1 | # MONGODB 2 | Spring Integration MongoDB support 3 | #### Version 4 4 | Examples using Spring Integration 4 release: 5 | 6 | * [mongo-basic] - Example with Java DSL on how to configure MongoDB outbound and inbound channel adapters. 7 | * Blog post: http://xpadro.com/2016/11/spring-integration-mongodb-adapters.html 8 | 9 | * [mongo-gateway] - Implementation of MongoDb Outbound Gateway with example application on how to use it. 10 | * Blog post: http://xpadro.com/2016/12/making-queries-on-demand-mongodb.html 11 | 12 | [mongo-basic]: https://github.com/xpadro/spring-integration/tree/master/mongodb/mongo-basic 13 | [mongo-gateway]: https://github.com/xpadro/spring-integration/tree/master/mongodb/mongo-gateway 14 | -------------------------------------------------------------------------------- /mongodb/mongo-basic/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | .mvn 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | 12 | ### IntelliJ IDEA ### 13 | .idea 14 | *.iws 15 | *.iml 16 | *.ipr 17 | 18 | ### NetBeans ### 19 | nbproject/private/ 20 | build/ 21 | nbbuild/ 22 | dist/ 23 | nbdist/ 24 | .nb-gradle/ -------------------------------------------------------------------------------- /mongodb/mongo-basic/src/main/java/xpadro/spring/integration/mongodb/MongodbBasicApplication.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.mongodb; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.context.ConfigurableApplicationContext; 6 | import org.springframework.integration.config.EnableIntegration; 7 | import xpadro.spring.integration.mongodb.configuration.InfrastructureConfiguration; 8 | import xpadro.spring.integration.mongodb.entity.Order; 9 | import xpadro.spring.integration.mongodb.repository.ProductRepository; 10 | 11 | @SpringBootApplication 12 | @EnableIntegration 13 | public class MongodbBasicApplication { 14 | 15 | public static void main(String[] args) { 16 | ConfigurableApplicationContext context = SpringApplication.run(MongodbBasicApplication.class, args); 17 | new MongodbBasicApplication().start(context); 18 | } 19 | 20 | public void start(ConfigurableApplicationContext context) { 21 | resetDatabase(context); 22 | 23 | Order order1 = new Order("1", true); 24 | Order order2 = new Order("2", false); 25 | Order order3 = new Order("3", true); 26 | 27 | InfrastructureConfiguration.OrderService orderService = context.getBean(InfrastructureConfiguration.OrderService.class); 28 | 29 | orderService.order(order1); 30 | orderService.order(order2); 31 | orderService.order(order3); 32 | } 33 | 34 | private void resetDatabase(ConfigurableApplicationContext context) { 35 | ProductRepository productRepository = context.getBean(ProductRepository.class); 36 | productRepository.deleteAll(); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /mongodb/mongo-basic/src/main/java/xpadro/spring/integration/mongodb/endpoint/OrderToProductConverter.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.mongodb.endpoint; 2 | 3 | import org.springframework.core.convert.converter.Converter; 4 | import xpadro.spring.integration.mongodb.entity.Order; 5 | import xpadro.spring.integration.mongodb.entity.Product; 6 | 7 | public class OrderToProductConverter implements Converter { 8 | 9 | @Override 10 | public Product convert(Order order) { 11 | return new Product(order.getId(), order.isPremium()); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /mongodb/mongo-basic/src/main/java/xpadro/spring/integration/mongodb/endpoint/ProductProcessor.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.mongodb.endpoint; 2 | 3 | import xpadro.spring.integration.mongodb.entity.Product; 4 | 5 | public class ProductProcessor { 6 | 7 | public Product process(Product product) { 8 | return doProcess(product, String.format("Processing product %s", product.getId())); 9 | } 10 | 11 | public Product fastProcess(Product product) { 12 | return doProcess(product, String.format("Fast processing product %s", product.getId())); 13 | } 14 | 15 | private Product doProcess(Product product, String message) { 16 | System.out.println(message); 17 | product.setProcessed(true); 18 | return product; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /mongodb/mongo-basic/src/main/java/xpadro/spring/integration/mongodb/entity/Order.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.mongodb.entity; 2 | 3 | public class Order { 4 | private final String id; 5 | private final boolean premium; 6 | 7 | public Order(String id, boolean premium) { 8 | this.id = id; 9 | this.premium = premium; 10 | } 11 | 12 | public String getId() { 13 | return id; 14 | } 15 | 16 | public boolean isPremium() { 17 | return premium; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /mongodb/mongo-basic/src/main/java/xpadro/spring/integration/mongodb/entity/Product.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.mongodb.entity; 2 | 3 | public class Product { 4 | private static final int DAYS_STANDARD = 5; 5 | private static final int DAYS_PREMIUM = 3; 6 | 7 | private String id; 8 | private boolean premium; 9 | private int daysToDeliver; 10 | private boolean processed; 11 | 12 | 13 | public Product(String id, boolean premium) { 14 | this.id = id; 15 | this.premium = premium; 16 | this.daysToDeliver = premium ? DAYS_PREMIUM : DAYS_STANDARD; 17 | } 18 | 19 | public Product() { 20 | } 21 | 22 | public String getId() { 23 | return id; 24 | } 25 | 26 | public boolean isPremium() { 27 | return premium; 28 | } 29 | 30 | public int getDaysToDeliver() { 31 | return daysToDeliver; 32 | } 33 | 34 | public boolean isProcessed() { 35 | return processed; 36 | } 37 | 38 | public void setProcessed(boolean processed) { 39 | this.processed = processed; 40 | } 41 | 42 | @Override 43 | public String toString() { 44 | return "Product{" + 45 | "id='" + id + '\'' + 46 | ", premium=" + premium + 47 | ", daysToDeliver=" + daysToDeliver + 48 | '}'; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /mongodb/mongo-basic/src/main/java/xpadro/spring/integration/mongodb/repository/ProductRepository.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.mongodb.repository; 2 | 3 | import org.springframework.data.mongodb.repository.MongoRepository; 4 | import org.springframework.stereotype.Repository; 5 | import xpadro.spring.integration.mongodb.entity.Product; 6 | 7 | @Repository 8 | public interface ProductRepository extends MongoRepository { 9 | 10 | } 11 | -------------------------------------------------------------------------------- /mongodb/mongo-basic/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.data.mongodb.database=simongo -------------------------------------------------------------------------------- /mongodb/mongo-basic/src/test/java/xpadro/spring/integration/mongodb/MongodbBasicApplicationTests.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.mongodb; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class MongodbBasicApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /mongodb/mongo-gateway/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | .mvn 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | 12 | ### IntelliJ IDEA ### 13 | .idea 14 | *.iws 15 | *.iml 16 | *.ipr 17 | 18 | ### NetBeans ### 19 | nbproject/private/ 20 | build/ 21 | nbbuild/ 22 | dist/ 23 | nbdist/ 24 | .nb-gradle/ -------------------------------------------------------------------------------- /mongodb/mongo-gateway/src/main/java/xpadro/spring/integration/mongodb/example/AbstractApplication.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.mongodb.example; 2 | 3 | import org.springframework.context.ConfigurableApplicationContext; 4 | import xpadro.spring.integration.mongodb.example.domain.Person; 5 | import xpadro.spring.integration.mongodb.example.domain.PersonRepository; 6 | 7 | /** 8 | * @author Xavier Padró 9 | */ 10 | public class AbstractApplication { 11 | 12 | protected void resetDatabase(ConfigurableApplicationContext context) { 13 | PersonRepository personRepository = context.getBean(PersonRepository.class); 14 | personRepository.deleteAll(); 15 | 16 | personRepository.save(new Person(1, "John")); 17 | personRepository.save(new Person(2, "Anna")); 18 | personRepository.save(new Person(3, "Xavi")); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /mongodb/mongo-gateway/src/main/java/xpadro/spring/integration/mongodb/example/JavaConfigQueryApplication.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.mongodb.example; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.context.ConfigurableApplicationContext; 6 | import org.springframework.context.annotation.Import; 7 | import org.springframework.integration.config.EnableIntegration; 8 | import xpadro.spring.integration.mongodb.example.configuration.JavaConfigQueryConfiguration; 9 | import xpadro.spring.integration.mongodb.example.configuration.JavaDSLQueryConfiguration; 10 | import xpadro.spring.integration.mongodb.example.domain.RequestMessage; 11 | 12 | /** 13 | * @author Xavier Padró 14 | */ 15 | @SpringBootApplication 16 | @EnableIntegration 17 | @Import(JavaConfigQueryConfiguration.class) 18 | public class JavaConfigQueryApplication extends AbstractApplication { 19 | 20 | public static void main(String[] args) { 21 | ConfigurableApplicationContext context = SpringApplication.run(JavaConfigQueryApplication.class, args); 22 | new JavaConfigQueryApplication().start(context); 23 | } 24 | 25 | public void start(ConfigurableApplicationContext context) { 26 | resetDatabase(context); 27 | 28 | JavaDSLQueryConfiguration.PersonService personService = context.getBean(JavaDSLQueryConfiguration.PersonService.class); 29 | personService.send(new RequestMessage("{id : 3}")); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /mongodb/mongo-gateway/src/main/java/xpadro/spring/integration/mongodb/example/JavaDSLQueryApplication.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.mongodb.example; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.context.ConfigurableApplicationContext; 6 | import org.springframework.context.annotation.Import; 7 | import org.springframework.integration.config.EnableIntegration; 8 | import xpadro.spring.integration.mongodb.example.configuration.JavaDSLQueryConfiguration; 9 | import xpadro.spring.integration.mongodb.example.domain.RequestMessage; 10 | 11 | /** 12 | * @author Xavier Padró 13 | */ 14 | @SpringBootApplication 15 | @EnableIntegration 16 | @Import(JavaDSLQueryConfiguration.class) 17 | public class JavaDSLQueryApplication extends AbstractApplication { 18 | 19 | public static void main(String[] args) { 20 | ConfigurableApplicationContext context = SpringApplication.run(JavaDSLQueryApplication.class, args); 21 | new JavaDSLQueryApplication().start(context); 22 | } 23 | 24 | public void start(ConfigurableApplicationContext context) { 25 | resetDatabase(context); 26 | 27 | JavaDSLQueryConfiguration.PersonService personService = context.getBean(JavaDSLQueryConfiguration.PersonService.class); 28 | personService.send(new RequestMessage()); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /mongodb/mongo-gateway/src/main/java/xpadro/spring/integration/mongodb/example/JavaDSLQueryExpressionApplication.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.mongodb.example; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.context.ConfigurableApplicationContext; 6 | import org.springframework.context.annotation.Import; 7 | import org.springframework.integration.config.EnableIntegration; 8 | import xpadro.spring.integration.mongodb.example.configuration.JavaDSLQueryConfiguration; 9 | import xpadro.spring.integration.mongodb.example.configuration.JavaDSLQueryExpressionConfiguration; 10 | import xpadro.spring.integration.mongodb.example.domain.RequestMessage; 11 | 12 | /** 13 | * @author Xavier Padró 14 | */ 15 | @SpringBootApplication 16 | @EnableIntegration 17 | @Import(JavaDSLQueryExpressionConfiguration.class) 18 | public class JavaDSLQueryExpressionApplication extends AbstractApplication { 19 | 20 | public static void main(String[] args) { 21 | ConfigurableApplicationContext context = SpringApplication.run(JavaDSLQueryExpressionApplication.class, args); 22 | new JavaDSLQueryExpressionApplication().start(context); 23 | } 24 | 25 | public void start(ConfigurableApplicationContext context) { 26 | resetDatabase(context); 27 | 28 | JavaDSLQueryConfiguration.PersonService personService = context.getBean(JavaDSLQueryConfiguration.PersonService.class); 29 | personService.send(new RequestMessage("{id : 2}")); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /mongodb/mongo-gateway/src/main/java/xpadro/spring/integration/mongodb/example/JavaDSLQueryExpressionListApplication.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.mongodb.example; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.context.ConfigurableApplicationContext; 6 | import org.springframework.context.annotation.Import; 7 | import org.springframework.integration.config.EnableIntegration; 8 | import xpadro.spring.integration.mongodb.example.configuration.JavaDSLQueryConfiguration; 9 | import xpadro.spring.integration.mongodb.example.configuration.JavaDSLQueryExpressionConfiguration; 10 | import xpadro.spring.integration.mongodb.example.configuration.JavaDSLQueryExpressionListConfiguration; 11 | import xpadro.spring.integration.mongodb.example.domain.RequestMessage; 12 | 13 | /** 14 | * @author Xavier Padró 15 | */ 16 | @SpringBootApplication 17 | @EnableIntegration 18 | @Import(JavaDSLQueryExpressionListConfiguration.class) 19 | public class JavaDSLQueryExpressionListApplication extends AbstractApplication { 20 | 21 | public static void main(String[] args) { 22 | ConfigurableApplicationContext context = SpringApplication.run(JavaDSLQueryExpressionListApplication.class, args); 23 | new JavaDSLQueryExpressionListApplication().start(context); 24 | } 25 | 26 | public void start(ConfigurableApplicationContext context) { 27 | resetDatabase(context); 28 | 29 | JavaDSLQueryConfiguration.PersonService personService = context.getBean(JavaDSLQueryConfiguration.PersonService.class); 30 | personService.send(new RequestMessage("{}")); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /mongodb/mongo-gateway/src/main/java/xpadro/spring/integration/mongodb/example/domain/Person.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.mongodb.example.domain; 2 | 3 | /** 4 | * @author Xavier Padró 5 | */ 6 | public class Person { 7 | private int id; 8 | private String name; 9 | 10 | public Person() { 11 | } 12 | 13 | public Person(int id, String name) { 14 | this.id = id; 15 | this.name = name; 16 | } 17 | 18 | public int getId() { 19 | return id; 20 | } 21 | 22 | public void setId(int id) { 23 | this.id = id; 24 | } 25 | 26 | public String getName() { 27 | return name; 28 | } 29 | 30 | public void setName(String name) { 31 | this.name = name; 32 | } 33 | 34 | @Override 35 | public String toString() { 36 | return "{" + 37 | "id=" + id + 38 | ", name='" + name + '\'' + 39 | '}'; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /mongodb/mongo-gateway/src/main/java/xpadro/spring/integration/mongodb/example/domain/PersonRepository.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.mongodb.example.domain; 2 | 3 | import org.springframework.data.mongodb.repository.MongoRepository; 4 | import org.springframework.stereotype.Repository; 5 | 6 | /** 7 | * @author Xavier Padró 8 | */ 9 | @Repository 10 | public interface PersonRepository extends MongoRepository { 11 | 12 | } 13 | -------------------------------------------------------------------------------- /mongodb/mongo-gateway/src/main/java/xpadro/spring/integration/mongodb/example/domain/RequestMessage.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.mongodb.example.domain; 2 | 3 | /** 4 | * @author Xavier Padró 5 | */ 6 | public class RequestMessage { 7 | private String data; 8 | 9 | public RequestMessage() { } 10 | 11 | public RequestMessage(String data) { 12 | this.data = data; 13 | } 14 | 15 | public String getData() { 16 | return data; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /mongodb/mongo-gateway/src/main/java/xpadro/spring/integration/mongodb/example/domain/ResultHandler.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.mongodb.example.domain; 2 | 3 | import java.util.List; 4 | import java.util.stream.Collectors; 5 | 6 | /** 7 | * @author Xavier Padró 8 | */ 9 | public class ResultHandler { 10 | 11 | public void handle(Person person) { 12 | System.out.println(String.format("Person retrieved: %s", person)); 13 | } 14 | 15 | public void handle(List persons) { 16 | String names = persons.stream().map(Person::getName).collect(Collectors.joining(", ")); 17 | System.out.println(String.format("Persons retrieved: %s", names)); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /mongodb/mongo-gateway/src/main/java/xpadro/spring/integration/mongodb/example/domain/ResultServiceActivator.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.mongodb.example.domain; 2 | 3 | import org.springframework.integration.annotation.ServiceActivator; 4 | 5 | /** 6 | * @author Xavier Padró 7 | */ 8 | public class ResultServiceActivator { 9 | 10 | @ServiceActivator(inputChannel = "personOutput") 11 | public void handle(Person person) { 12 | System.out.println(String.format("Person retrieved: %s", person)); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /mongodb/mongo-gateway/src/main/java/xpadro/spring/integration/mongodb/gateway/MongoDb.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.mongodb.gateway; 2 | 3 | import org.springframework.data.mongodb.MongoDbFactory; 4 | import org.springframework.data.mongodb.core.MongoTemplate; 5 | 6 | /** 7 | * Factory class for building MongoDb components 8 | * 9 | * @author Xavier Padró 10 | */ 11 | public class MongoDb { 12 | 13 | public static MongoDbOutboundGatewaySpec outboundGateway(MongoDbFactory mongoDbFactory) { 14 | return new MongoDbOutboundGatewaySpec(new MongoDbExecutor(mongoDbFactory)); 15 | } 16 | 17 | public static MongoDbOutboundGatewaySpec outboundGateway(MongoTemplate mongoTemplate) { 18 | return new MongoDbOutboundGatewaySpec(new MongoDbExecutor(mongoTemplate)); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /mongodb/mongo-gateway/src/main/java/xpadro/spring/integration/mongodb/gateway/MongoDbOutboundGateway.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.mongodb.gateway; 2 | 3 | import org.springframework.integration.handler.AbstractReplyProducingMessageHandler; 4 | import org.springframework.messaging.Message; 5 | import org.springframework.util.Assert; 6 | 7 | /** 8 | * Makes outbound operations to query a MongoDb database using a {@link MongoDbExecutor} 9 | * 10 | * @author Xavier Padró 11 | */ 12 | public class MongoDbOutboundGateway extends AbstractReplyProducingMessageHandler { 13 | private final MongoDbExecutor mongoDbExecutor; 14 | 15 | private volatile boolean initialized = false; 16 | 17 | public MongoDbOutboundGateway(MongoDbExecutor mongoDbExecutor) { 18 | Assert.notNull(mongoDbExecutor, "mongoDbExecutor must not be null."); 19 | 20 | this.mongoDbExecutor = mongoDbExecutor; 21 | } 22 | 23 | @Override 24 | protected void doInit() { 25 | this.mongoDbExecutor.setBeanFactory(this.getBeanFactory()); 26 | this.initialized = true; 27 | } 28 | 29 | @Override 30 | protected Object handleRequestMessage(Message requestMessage) { 31 | Assert.isTrue(this.initialized, "This class is not yet initialized. Invoke its afterPropertiesSet() method"); 32 | 33 | Object result = this.mongoDbExecutor.execute(requestMessage); 34 | 35 | return this.getMessageBuilderFactory().withPayload(result).copyHeaders(requestMessage.getHeaders()).build(); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /mongodb/mongo-gateway/src/main/java/xpadro/spring/integration/mongodb/gateway/MongoDbOutboundGatewayFactoryBean.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.mongodb.gateway; 2 | 3 | import org.springframework.beans.factory.config.AbstractFactoryBean; 4 | import org.springframework.messaging.MessageHandler; 5 | 6 | /** 7 | * Creates instances of {@link MongoDbOutboundGateway} 8 | * 9 | * @author Xavier Padró 10 | */ 11 | public class MongoDbOutboundGatewayFactoryBean extends AbstractFactoryBean { 12 | private MongoDbExecutor mongoDbExecutor; 13 | 14 | public void setMongoDbExecutor(MongoDbExecutor mongoDbExecutor) { 15 | this.mongoDbExecutor = mongoDbExecutor; 16 | } 17 | 18 | private boolean requiresReply = false; 19 | 20 | public void setRequiresReply(boolean requiresReply) { 21 | this.requiresReply = requiresReply; 22 | } 23 | 24 | @Override 25 | public Class getObjectType() { 26 | return MessageHandler.class; 27 | } 28 | 29 | @Override 30 | protected MessageHandler createInstance() throws Exception { 31 | MongoDbOutboundGateway mongoDbOutboundGateway = new MongoDbOutboundGateway(this.mongoDbExecutor); 32 | 33 | mongoDbOutboundGateway.setRequiresReply(this.requiresReply); 34 | mongoDbOutboundGateway.setBeanFactory(this.getBeanFactory()); 35 | 36 | mongoDbOutboundGateway.afterPropertiesSet(); 37 | 38 | return mongoDbOutboundGateway; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /mongodb/mongo-gateway/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.data.mongodb.database=simongo -------------------------------------------------------------------------------- /rmi/README.md: -------------------------------------------------------------------------------- 1 | # RMI 2 | Spring Integration RMI support 3 | #### Version 2 4 | Examples using Spring Integration 2 release: 5 | 6 | * [rmi-basic] - This project shows a client invoking a service over RMI with Spring Integration. The first client test class uses the MessagingTemplate to explicitly send its request to the message channel. The second client shows how to do it transparently with gateways. 7 | * Blog post: http://xpadro.com/2013/10/spring-integration-using-rmi-channel.html 8 | 9 | 10 | [rmi-basic]: https://github.com/xpadro/spring-integration/tree/master/rmi/rmi-basic 11 | 12 | -------------------------------------------------------------------------------- /rmi/rmi-basic/src/main/java/xpadro/spring/integration/launcher/ServerLauncher.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.launcher; 2 | 3 | import org.springframework.context.ApplicationContext; 4 | import org.springframework.context.support.ClassPathXmlApplicationContext; 5 | 6 | public class ServerLauncher { 7 | 8 | public static void main(String[] args) { 9 | ApplicationContext context = new ClassPathXmlApplicationContext("xpadro/spring/integration/config/server-config.xml"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /rmi/rmi-basic/src/main/java/xpadro/spring/integration/model/Employee.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.model; 2 | 3 | import java.io.Serializable; 4 | 5 | public class Employee implements Serializable { 6 | private static final long serialVersionUID = 1L; 7 | 8 | private int id; 9 | private String name; 10 | 11 | 12 | public int getId() { 13 | return id; 14 | } 15 | 16 | public void setId(int id) { 17 | this.id = id; 18 | } 19 | 20 | public String getName() { 21 | return name; 22 | } 23 | 24 | public void setName(String name) { 25 | this.name = name; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /rmi/rmi-basic/src/main/java/xpadro/spring/integration/repository/EmployeeRepository.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.repository; 2 | 3 | import xpadro.spring.integration.model.Employee; 4 | 5 | public interface EmployeeRepository { 6 | Employee getEmployee(int id); 7 | } 8 | -------------------------------------------------------------------------------- /rmi/rmi-basic/src/main/java/xpadro/spring/integration/repository/EmployeeRepositoryImpl.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.repository; 2 | 3 | import java.sql.ResultSet; 4 | import java.sql.SQLException; 5 | 6 | import javax.sql.DataSource; 7 | 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.jdbc.core.JdbcTemplate; 10 | import org.springframework.jdbc.core.RowMapper; 11 | import org.springframework.stereotype.Repository; 12 | 13 | import xpadro.spring.integration.model.Employee; 14 | 15 | 16 | @Repository 17 | public class EmployeeRepositoryImpl implements EmployeeRepository { 18 | private JdbcTemplate template; 19 | private RowMapper rowMapper = new EmployeeRowMapper(); 20 | private static final String SEARCH = "select * from employees where id = ?"; 21 | private static final String COLUMN_ID = "id"; 22 | private static final String COLUMN_NAME = "name"; 23 | 24 | @Autowired 25 | public EmployeeRepositoryImpl(DataSource dataSource) { 26 | this.template = new JdbcTemplate(dataSource); 27 | } 28 | 29 | public Employee getEmployee(int id) { 30 | return template.queryForObject(SEARCH, rowMapper, id); 31 | } 32 | 33 | private class EmployeeRowMapper implements RowMapper { 34 | public Employee mapRow(ResultSet rs, int i) throws SQLException { 35 | Employee employee = new Employee(); 36 | employee.setId(rs.getInt(COLUMN_ID)); 37 | employee.setName(rs.getString(COLUMN_NAME)); 38 | 39 | return employee; 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /rmi/rmi-basic/src/main/java/xpadro/spring/integration/service/EmployeeService.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.service; 2 | 3 | import xpadro.spring.integration.model.Employee; 4 | 5 | public interface EmployeeService { 6 | Employee retrieveEmployee(int id); 7 | } 8 | -------------------------------------------------------------------------------- /rmi/rmi-basic/src/main/java/xpadro/spring/integration/service/EmployeeServiceImpl.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.service; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Service; 5 | 6 | import xpadro.spring.integration.model.Employee; 7 | import xpadro.spring.integration.repository.EmployeeRepository; 8 | 9 | @Service("defaultEmployeeService") 10 | public class EmployeeServiceImpl implements EmployeeService { 11 | @Autowired 12 | private EmployeeRepository employeeRepository; 13 | 14 | @Override 15 | public Employee retrieveEmployee(int id) { 16 | return employeeRepository.getEmployee(id); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /rmi/rmi-basic/src/main/resources/db/schemas/data.sql: -------------------------------------------------------------------------------- 1 | insert into employees (id, name) values (1, 'Mark Knopfler'); 2 | insert into employees (id, name) values (2, 'Bruce Springsteen'); 3 | insert into employees (id, name) values (3, 'Bono'); -------------------------------------------------------------------------------- /rmi/rmi-basic/src/main/resources/db/schemas/schema.sql: -------------------------------------------------------------------------------- 1 | drop table employees if exists; 2 | 3 | create table employees (id integer identity primary key, name varchar(255)); -------------------------------------------------------------------------------- /rmi/rmi-basic/src/main/resources/xpadro/spring/integration/config/server-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /rmi/rmi-basic/src/test/java/xpadro/spring/integration/client/TestRmiClient.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.client; 2 | 3 | import junit.framework.Assert; 4 | 5 | import org.junit.Test; 6 | import org.junit.runner.RunWith; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.integration.MessageChannel; 9 | import org.springframework.integration.core.MessagingTemplate; 10 | import org.springframework.test.context.ContextConfiguration; 11 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 12 | 13 | import xpadro.spring.integration.model.Employee; 14 | 15 | @RunWith(SpringJUnit4ClassRunner.class) 16 | @ContextConfiguration(locations={"classpath:xpadro/spring/integration/test/config/client-config.xml"}) 17 | public class TestRmiClient { 18 | @Autowired 19 | MessageChannel localChannel; 20 | 21 | @Autowired 22 | MessagingTemplate template; 23 | 24 | @Test 25 | public void retrieveExistingEmployee() { 26 | Employee employee = (Employee) template.convertSendAndReceive(localChannel, 2); 27 | 28 | Assert.assertNotNull(employee); 29 | Assert.assertEquals(2, employee.getId()); 30 | Assert.assertEquals("Bruce Springsteen", employee.getName()); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /rmi/rmi-basic/src/test/java/xpadro/spring/integration/client/TestRmiGatewayClient.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.client; 2 | 3 | import junit.framework.Assert; 4 | 5 | import org.junit.Test; 6 | import org.junit.runner.RunWith; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.test.context.ContextConfiguration; 9 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 10 | 11 | import xpadro.spring.integration.model.Employee; 12 | import xpadro.spring.integration.service.EmployeeService; 13 | 14 | @RunWith(SpringJUnit4ClassRunner.class) 15 | @ContextConfiguration(locations={"classpath:xpadro/spring/integration/test/config/client-gateway-config.xml"}) 16 | public class TestRmiGatewayClient { 17 | @Autowired 18 | private EmployeeService service; 19 | 20 | @Test 21 | public void retrieveExistingEmployee() { 22 | Employee employee = service.retrieveEmployee(2); 23 | Assert.assertNotNull(employee); 24 | Assert.assertEquals(2, employee.getId()); 25 | Assert.assertEquals("Bruce Springsteen", employee.getName()); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /rmi/rmi-basic/src/test/resources/xpadro/spring/integration/test/config/client-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /rmi/rmi-basic/src/test/resources/xpadro/spring/integration/test/config/client-gateway-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 12 | 13 | 14 | 15 | 17 | 18 | -------------------------------------------------------------------------------- /spring-jms/README.md: -------------------------------------------------------------------------------- 1 | # SPRING-JMS 2 | Spring JMS Examples 3 | #### Version 4 4 | Examples using Spring 4 release: 5 | 6 | * [jms-boot-javaconfig] - A simple example using annotated JMS listeners using no XML configuration. Spring Boot automatically configures the project based on dependencies included in the classpath. 7 | * Blog post: http://xpadro.com/2015/04/configure-spring-jms-application-with.html 8 | 9 | #### Version 3 10 | Examples using Spring 3 release: 11 | 12 | * [jms-basic] - Shows how to configure a basic Spring JMS application, with examples on how to send and receive messages using point-to-point messaging (queues) and publish-subscribe messaging (topics). 13 | * Blog post: http://xpadro.com/2013/07/introduction-to-messaging-with-spring.html 14 | 15 | * [jms-tx] - Contains examples reproducing the problems related to message processing and how to solve them with local JMS transactions. 16 | * Blog post: http://xpadro.com/2013/08/spring-jms-processing-messages-within.html 17 | 18 | 19 | 20 | [jms-basic]: https://github.com/xpadro/spring-integration/tree/master/spring-jms/jms-basic 21 | [jms-tx]: https://github.com/xpadro/spring-integration/tree/master/spring-jms/jms-tx 22 | [jms-boot-javaconfig]: https://github.com/xpadro/spring-integration/tree/master/spring-jms/jms-boot-javaconfig 23 | 24 | -------------------------------------------------------------------------------- /spring-jms/jms-basic/src/main/java/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /spring-jms/jms-basic/src/main/java/xpadro/spring/jms/model/Notification.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.jms.model; 2 | 3 | import java.io.Serializable; 4 | 5 | public class Notification implements Serializable { 6 | private static final long serialVersionUID = 1L; 7 | 8 | private String id; 9 | private String message; 10 | 11 | 12 | public Notification(String id, String message) { 13 | this.id = id; 14 | this.message = message; 15 | } 16 | 17 | /** 18 | * @return the id 19 | */ 20 | public String getId() { 21 | return id; 22 | } 23 | /** 24 | * @param id the id to set 25 | */ 26 | public void setId(String id) { 27 | this.id = id; 28 | } 29 | /** 30 | * @return the message 31 | */ 32 | public String getMessage() { 33 | return message; 34 | } 35 | /** 36 | * @param message the message to set 37 | */ 38 | public void setMessage(String message) { 39 | this.message = message; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /spring-jms/jms-basic/src/main/java/xpadro/spring/jms/producer/Producer.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.jms.producer; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.beans.factory.annotation.Qualifier; 5 | import org.springframework.jms.core.JmsTemplate; 6 | import org.springframework.stereotype.Component; 7 | 8 | import xpadro.spring.jms.model.Notification; 9 | 10 | @Component("producer") 11 | public class Producer { 12 | 13 | @Autowired 14 | @Qualifier("jmsTemplate") 15 | private JmsTemplate jmsTemplate; 16 | 17 | @Autowired 18 | @Qualifier("jmsTopicTemplate") 19 | private JmsTemplate jmsTopicTemplate; 20 | 21 | 22 | public void convertAndSendMessage(Notification notification) { 23 | jmsTemplate.convertAndSend(notification); 24 | } 25 | 26 | public void convertAndSendMessage(String destination, Notification notification) { 27 | jmsTemplate.convertAndSend(destination, notification); 28 | } 29 | 30 | public void convertAndSendTopic(Notification notification) { 31 | jmsTopicTemplate.convertAndSend("test.topic", notification); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /spring-jms/jms-basic/src/main/java/xpadro/spring/jms/receiver/AsyncReceiver.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.jms.receiver; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Component; 5 | 6 | import xpadro.spring.jms.model.Notification; 7 | 8 | @Component("asyncReceiver") 9 | public class AsyncReceiver { 10 | @Autowired 11 | private NotificationRegistry registry; 12 | 13 | public void receiveMessage(Notification notification) { 14 | registry.registerNotification(notification); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /spring-jms/jms-basic/src/main/java/xpadro/spring/jms/receiver/AsyncTopicBarReceiver.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.jms.receiver; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Component; 5 | 6 | import xpadro.spring.jms.model.Notification; 7 | 8 | @Component("asyncTopicBarReceiver") 9 | public class AsyncTopicBarReceiver { 10 | @Autowired 11 | private NotificationRegistry registry; 12 | 13 | public void receive(Notification notification) { 14 | registry.registerNotification(notification); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /spring-jms/jms-basic/src/main/java/xpadro/spring/jms/receiver/AsyncTopicFooReceiver.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.jms.receiver; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Component; 7 | 8 | import xpadro.spring.jms.model.Notification; 9 | 10 | @Component("asyncTopicFooReceiver") 11 | public class AsyncTopicFooReceiver { 12 | private Logger logger = LoggerFactory.getLogger(this.getClass()); 13 | 14 | @Autowired 15 | private NotificationRegistry registry; 16 | 17 | public void receive(Notification notification) { 18 | registry.registerNotification(notification); 19 | } 20 | 21 | 22 | public void receiveIteration(Notification notification) { 23 | registry.registerNotification(notification); 24 | logger.info("Receiving notification {}", notification.getId()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /spring-jms/jms-basic/src/main/java/xpadro/spring/jms/receiver/DynamicTopicReceiver.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.jms.receiver; 2 | 3 | import javax.jms.JMSException; 4 | import javax.jms.Message; 5 | import javax.jms.MessageListener; 6 | import javax.jms.ObjectMessage; 7 | 8 | import org.springframework.jms.support.JmsUtils; 9 | 10 | import xpadro.spring.jms.model.Notification; 11 | 12 | public class DynamicTopicReceiver implements MessageListener { 13 | private NotificationRegistry registry; 14 | 15 | public DynamicTopicReceiver() {} 16 | 17 | public DynamicTopicReceiver(NotificationRegistry registry) { 18 | this.registry = registry; 19 | } 20 | 21 | @Override 22 | public void onMessage(Message message) { 23 | try { 24 | Notification notification = (Notification) ((ObjectMessage) message).getObject(); 25 | registry.registerNotification(notification); 26 | }catch (JMSException e) { 27 | throw JmsUtils.convertJmsAccessException(e); 28 | } 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /spring-jms/jms-basic/src/main/java/xpadro/spring/jms/receiver/NotificationRegistry.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.jms.receiver; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import org.slf4j.Logger; 7 | import org.slf4j.LoggerFactory; 8 | import org.springframework.stereotype.Component; 9 | 10 | import xpadro.spring.jms.model.Notification; 11 | 12 | @Component("notificationRegistry") 13 | public class NotificationRegistry { 14 | private Logger logger = LoggerFactory.getLogger(this.getClass()); 15 | private List receivedNotifications = new ArrayList(); 16 | 17 | public List getReceivedNotifications() { 18 | return receivedNotifications; 19 | } 20 | 21 | public void registerNotification(Notification notification) { 22 | logger.info("inserting notification"); 23 | receivedNotifications.add(notification); 24 | } 25 | 26 | public void clear() { 27 | receivedNotifications.clear(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /spring-jms/jms-basic/src/main/java/xpadro/spring/jms/receiver/SyncReceiver.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.jms.receiver; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.jms.core.JmsTemplate; 5 | import org.springframework.stereotype.Component; 6 | 7 | import xpadro.spring.jms.model.Notification; 8 | 9 | @Component 10 | public class SyncReceiver { 11 | 12 | @Autowired 13 | private JmsTemplate jmsTemplate; 14 | 15 | public Notification receive() { 16 | return (Notification) jmsTemplate.receiveAndConvert("test.sync.queue"); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /spring-jms/jms-basic/src/main/resources/xpadro/spring/jms/config/app-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /spring-jms/jms-basic/src/main/resources/xpadro/spring/jms/config/jms-iter-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /spring-jms/jms-basic/src/test/java/xpadro/spring/jms/test/TestAsyncMessaging.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.jms.test; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import org.junit.Before; 6 | import org.junit.Test; 7 | import org.junit.runner.RunWith; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.test.context.ContextConfiguration; 10 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 11 | 12 | import xpadro.spring.jms.model.Notification; 13 | import xpadro.spring.jms.producer.Producer; 14 | import xpadro.spring.jms.receiver.NotificationRegistry; 15 | 16 | @ContextConfiguration(locations = { 17 | "/xpadro/spring/jms/config/jms-config.xml", 18 | "/xpadro/spring/jms/config/app-config.xml"}) 19 | @RunWith(SpringJUnit4ClassRunner.class) 20 | public class TestAsyncMessaging { 21 | 22 | @Autowired 23 | private Producer producer; 24 | 25 | @Autowired 26 | private NotificationRegistry registry; 27 | 28 | @Before 29 | public void prepareTest() { 30 | registry.clear(); 31 | } 32 | 33 | @Test 34 | public void testAsynchronizedReceiving() throws InterruptedException { 35 | Notification notification = new Notification("2", "this is another message"); 36 | producer.convertAndSendMessage("test.async.queue", notification); 37 | 38 | Thread.sleep(2000); 39 | 40 | assertEquals(1, registry.getReceivedNotifications().size()); 41 | assertEquals("this is another message", registry.getReceivedNotifications().get(0).getMessage()); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /spring-jms/jms-basic/src/test/java/xpadro/spring/jms/test/TestIterateToTopic.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.jms.test; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import org.junit.Before; 6 | import org.junit.Test; 7 | import org.junit.runner.RunWith; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.test.context.ContextConfiguration; 10 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 11 | 12 | import xpadro.spring.jms.model.Notification; 13 | import xpadro.spring.jms.producer.Producer; 14 | import xpadro.spring.jms.receiver.NotificationRegistry; 15 | 16 | @ContextConfiguration(locations = { 17 | "/xpadro/spring/jms/config/jms-iter-config.xml", 18 | "/xpadro/spring/jms/config/app-config.xml"}) 19 | @RunWith(SpringJUnit4ClassRunner.class) 20 | public class TestIterateToTopic { 21 | 22 | @Autowired 23 | private Producer producer; 24 | 25 | @Autowired 26 | private NotificationRegistry registry; 27 | 28 | @Before 29 | public void prepareTest() { 30 | registry.clear(); 31 | } 32 | 33 | @Test 34 | public void testTopicSending() throws InterruptedException { 35 | Notification notification = null; 36 | 37 | for (int i=1;i<11;i++) { 38 | notification = new Notification(String.valueOf(i), "this is a topic"); 39 | producer.convertAndSendTopic(notification); 40 | } 41 | 42 | Thread.sleep(2000); 43 | assertEquals(10, registry.getReceivedNotifications().size()); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /spring-jms/jms-basic/src/test/java/xpadro/spring/jms/test/TestSyncMessaging.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.jms.test; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | import static org.junit.Assert.assertNotNull; 5 | 6 | import java.rmi.RemoteException; 7 | 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.test.context.ContextConfiguration; 12 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 13 | 14 | import xpadro.spring.jms.model.Notification; 15 | import xpadro.spring.jms.producer.Producer; 16 | import xpadro.spring.jms.receiver.SyncReceiver; 17 | 18 | @ContextConfiguration(locations = { 19 | "/xpadro/spring/jms/config/jms-config.xml", 20 | "/xpadro/spring/jms/config/app-config.xml"}) 21 | @RunWith(SpringJUnit4ClassRunner.class) 22 | public class TestSyncMessaging { 23 | 24 | @Autowired 25 | private Producer producer; 26 | 27 | @Autowired 28 | private SyncReceiver syncReceiver; 29 | 30 | 31 | @Test 32 | public void testSynchronizedReceiving() throws InterruptedException, RemoteException { 33 | Notification notification = new Notification("1", "this is a message"); 34 | //Sends the message to the jmsTemplate's default destination 35 | producer.convertAndSendMessage(notification); 36 | 37 | Thread.sleep(2000); 38 | 39 | Notification receivedNotification = syncReceiver.receive(); 40 | assertNotNull(receivedNotification); 41 | assertEquals("this is a message", receivedNotification.getMessage()); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /spring-jms/jms-basic/src/test/java/xpadro/spring/jms/test/TestTopicMessaging.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.jms.test; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import org.junit.Before; 6 | import org.junit.Test; 7 | import org.junit.runner.RunWith; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.test.context.ContextConfiguration; 10 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 11 | 12 | import xpadro.spring.jms.model.Notification; 13 | import xpadro.spring.jms.producer.Producer; 14 | import xpadro.spring.jms.receiver.NotificationRegistry; 15 | 16 | @ContextConfiguration(locations = { 17 | "/xpadro/spring/jms/config/jms-config.xml", 18 | "/xpadro/spring/jms/config/app-config.xml"}) 19 | @RunWith(SpringJUnit4ClassRunner.class) 20 | public class TestTopicMessaging { 21 | 22 | @Autowired 23 | private Producer producer; 24 | 25 | @Autowired 26 | private NotificationRegistry registry; 27 | 28 | @Before 29 | public void prepareTest() { 30 | registry.clear(); 31 | } 32 | 33 | @Test 34 | public void testTopicSending() throws InterruptedException { 35 | Notification notification = new Notification("3", "this is a topic"); 36 | producer.convertAndSendTopic(notification); 37 | 38 | Thread.sleep(2000); 39 | 40 | assertEquals(2, registry.getReceivedNotifications().size()); 41 | assertEquals("this is a topic", registry.getReceivedNotifications().get(0).getMessage()); 42 | assertEquals("this is a topic", registry.getReceivedNotifications().get(1).getMessage()); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /spring-jms/jms-boot-javaconfig/activemq-data/embedded/KahaDB/db-1.log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xpadro/spring-integration/1e7743f1308b793123a8b7cd2171c20277e3a22f/spring-jms/jms-boot-javaconfig/activemq-data/embedded/KahaDB/db-1.log -------------------------------------------------------------------------------- /spring-jms/jms-boot-javaconfig/activemq-data/embedded/KahaDB/db.data: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xpadro/spring-integration/1e7743f1308b793123a8b7cd2171c20277e3a22f/spring-jms/jms-boot-javaconfig/activemq-data/embedded/KahaDB/db.data -------------------------------------------------------------------------------- /spring-jms/jms-boot-javaconfig/activemq-data/embedded/KahaDB/db.redo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xpadro/spring-integration/1e7743f1308b793123a8b7cd2171c20277e3a22f/spring-jms/jms-boot-javaconfig/activemq-data/embedded/KahaDB/db.redo -------------------------------------------------------------------------------- /spring-jms/jms-boot-javaconfig/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | xpadro.spring 7 | jms-boot-javaconfig 8 | 0.0.1-SNAPSHOT 9 | jar 10 | JMS Spring Boot Javaconfig 11 | 12 | 13 | org.springframework.boot 14 | spring-boot-starter-parent 15 | 1.2.3.RELEASE 16 | 17 | 18 | 19 | 20 | UTF-8 21 | xpadro.spring.jms.JmsJavaconfigApplication 22 | 1.8 23 | 5.4.2 24 | 25 | 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter 30 | 31 | 32 | org.springframework 33 | spring-jms 34 | 35 | 36 | 37 | org.springframework.boot 38 | spring-boot-starter-test 39 | test 40 | 41 | 42 | 43 | org.apache.activemq 44 | activemq-core 45 | ${amq.version} 46 | 47 | 48 | 49 | 50 | 51 | 52 | org.springframework.boot 53 | spring-boot-maven-plugin 54 | 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /spring-jms/jms-boot-javaconfig/src/main/java/xpadro/spring/jms/JmsJavaconfigApplication.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.jms; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class JmsJavaconfigApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(JmsJavaconfigApplication.class, args); 11 | } 12 | } -------------------------------------------------------------------------------- /spring-jms/jms-boot-javaconfig/src/main/java/xpadro/spring/jms/listener/InListener.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.jms.listener; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.jms.annotation.JmsListener; 5 | import org.springframework.messaging.handler.annotation.SendTo; 6 | import org.springframework.stereotype.Component; 7 | 8 | import xpadro.spring.jms.model.Order; 9 | import xpadro.spring.jms.service.StoreService; 10 | 11 | @Component 12 | public class InListener { 13 | private final StoreService storeService; 14 | 15 | @Autowired 16 | public InListener(StoreService storeService) { 17 | this.storeService = storeService; 18 | } 19 | 20 | @JmsListener(destination = "in.queue") 21 | @SendTo("out.queue") 22 | public String receiveOrder(Order order) { 23 | storeService.registerOrder(order); 24 | return order.getId(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /spring-jms/jms-boot-javaconfig/src/main/java/xpadro/spring/jms/listener/OutListener.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.jms.listener; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.jms.annotation.JmsListener; 5 | import org.springframework.stereotype.Component; 6 | 7 | import xpadro.spring.jms.service.RegisterService; 8 | 9 | @Component 10 | public class OutListener { 11 | private final RegisterService registerService; 12 | 13 | @Autowired 14 | public OutListener(RegisterService registerService) { 15 | this.registerService = registerService; 16 | } 17 | 18 | @JmsListener(destination = "out.queue") 19 | public void receiveOrder(String orderId) { 20 | registerService.registerOrderId(orderId); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /spring-jms/jms-boot-javaconfig/src/main/java/xpadro/spring/jms/listener/SimpleListener.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.jms.listener; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.jms.annotation.JmsListener; 5 | import org.springframework.stereotype.Component; 6 | 7 | import xpadro.spring.jms.model.Order; 8 | import xpadro.spring.jms.service.StoreService; 9 | 10 | @Component 11 | public class SimpleListener { 12 | private final StoreService storeService; 13 | 14 | @Autowired 15 | public SimpleListener(StoreService storeService) { 16 | this.storeService = storeService; 17 | } 18 | 19 | @JmsListener(destination = "simple.queue") 20 | public void receiveOrder(Order order) { 21 | storeService.registerOrder(order); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /spring-jms/jms-boot-javaconfig/src/main/java/xpadro/spring/jms/model/Order.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.jms.model; 2 | 3 | import java.io.Serializable; 4 | 5 | public class Order implements Serializable { 6 | private static final long serialVersionUID = -797586847427389162L; 7 | 8 | private final String id; 9 | 10 | public Order(String id) { 11 | this.id = id; 12 | } 13 | 14 | public String getId() { 15 | return id; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /spring-jms/jms-boot-javaconfig/src/main/java/xpadro/spring/jms/service/ClientService.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.jms.service; 2 | 3 | import xpadro.spring.jms.model.Order; 4 | 5 | public interface ClientService { 6 | 7 | void addOrder(Order order); 8 | 9 | void registerOrder(Order order); 10 | } 11 | -------------------------------------------------------------------------------- /spring-jms/jms-boot-javaconfig/src/main/java/xpadro/spring/jms/service/ClientServiceImpl.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.jms.service; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.jms.core.JmsTemplate; 5 | import org.springframework.stereotype.Service; 6 | 7 | import xpadro.spring.jms.model.Order; 8 | 9 | @Service 10 | public class ClientServiceImpl implements ClientService { 11 | private static final String SIMPLE_QUEUE = "simple.queue"; 12 | private static final String IN_QUEUE = "in.queue"; 13 | 14 | private final JmsTemplate jmsTemplate; 15 | 16 | @Autowired 17 | public ClientServiceImpl(JmsTemplate jmsTemplate) { 18 | this.jmsTemplate = jmsTemplate; 19 | } 20 | 21 | @Override 22 | public void addOrder(Order order) { 23 | jmsTemplate.convertAndSend(SIMPLE_QUEUE, order); 24 | } 25 | 26 | @Override 27 | public void registerOrder(Order order) { 28 | jmsTemplate.convertAndSend(IN_QUEUE, order); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /spring-jms/jms-boot-javaconfig/src/main/java/xpadro/spring/jms/service/RegisterService.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.jms.service; 2 | 3 | public interface RegisterService { 4 | void registerOrderId(String orderId); 5 | 6 | String getLastReceivedOrderId(); 7 | } 8 | -------------------------------------------------------------------------------- /spring-jms/jms-boot-javaconfig/src/main/java/xpadro/spring/jms/service/RegisterServiceImpl.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.jms.service; 2 | 3 | import org.springframework.stereotype.Service; 4 | 5 | @Service 6 | public class RegisterServiceImpl implements RegisterService { 7 | private String lastReceivedOrder; 8 | 9 | @Override 10 | public void registerOrderId(String orderId) { 11 | this.lastReceivedOrder = orderId; 12 | } 13 | 14 | @Override 15 | public String getLastReceivedOrderId() { 16 | return this.lastReceivedOrder; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /spring-jms/jms-boot-javaconfig/src/main/java/xpadro/spring/jms/service/StoreService.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.jms.service; 2 | 3 | import java.util.Optional; 4 | 5 | import xpadro.spring.jms.model.Order; 6 | 7 | public interface StoreService { 8 | 9 | void registerOrder(Order order); 10 | 11 | Optional getReceivedOrder(String id); 12 | } 13 | -------------------------------------------------------------------------------- /spring-jms/jms-boot-javaconfig/src/main/java/xpadro/spring/jms/service/StoreServiceImpl.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.jms.service; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.Optional; 6 | 7 | import org.springframework.stereotype.Service; 8 | 9 | import xpadro.spring.jms.model.Order; 10 | 11 | @Service 12 | public class StoreServiceImpl implements StoreService { 13 | private final List receivedOrders = new ArrayList<>(); 14 | 15 | @Override 16 | public void registerOrder(Order order) { 17 | this.receivedOrders.add(order); 18 | } 19 | 20 | @Override 21 | public Optional getReceivedOrder(String id) { 22 | return receivedOrders.stream().filter(o -> o.getId().equals(id)).findFirst(); 23 | } 24 | } -------------------------------------------------------------------------------- /spring-jms/jms-boot-javaconfig/src/test/java/xpadro/spring/jms/InOutListenerTest.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.jms; 2 | 3 | 4 | import java.util.Optional; 5 | 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.boot.test.SpringApplicationConfiguration; 11 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 12 | 13 | import xpadro.spring.jms.model.Order; 14 | import xpadro.spring.jms.service.ClientService; 15 | import xpadro.spring.jms.service.RegisterService; 16 | import xpadro.spring.jms.service.StoreService; 17 | 18 | @RunWith(SpringJUnit4ClassRunner.class) 19 | @SpringApplicationConfiguration(classes = JmsJavaconfigApplication.class) 20 | public class InOutListenerTest { 21 | 22 | @Autowired 23 | private ClientService clientService; 24 | 25 | @Autowired 26 | private StoreService storeService; 27 | 28 | @Autowired 29 | private RegisterService registerService; 30 | 31 | @Test 32 | public void registerOrder() throws InterruptedException { 33 | clientService.registerOrder(new Order("order1")); 34 | Thread.sleep(1000); 35 | 36 | Optional storedOrder = storeService.getReceivedOrder("order1"); 37 | Assert.assertTrue(storedOrder.isPresent()); 38 | Assert.assertEquals("order1", storedOrder.get().getId()); 39 | Assert.assertEquals("order1", registerService.getLastReceivedOrderId()); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /spring-jms/jms-boot-javaconfig/src/test/java/xpadro/spring/jms/SimpleListenerTest.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.jms; 2 | 3 | 4 | import java.util.Optional; 5 | 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.boot.test.SpringApplicationConfiguration; 11 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 12 | 13 | import xpadro.spring.jms.model.Order; 14 | import xpadro.spring.jms.service.ClientService; 15 | import xpadro.spring.jms.service.StoreService; 16 | 17 | 18 | @RunWith(SpringJUnit4ClassRunner.class) 19 | @SpringApplicationConfiguration(classes = JmsJavaconfigApplication.class) 20 | public class SimpleListenerTest { 21 | 22 | @Autowired 23 | private ClientService clientService; 24 | 25 | @Autowired 26 | private StoreService storeService; 27 | 28 | @Test 29 | public void sendSimpleMessage() throws InterruptedException { 30 | clientService.addOrder(new Order("order1")); 31 | Thread.sleep(500); 32 | 33 | Optional storedOrder = storeService.getReceivedOrder("order1"); 34 | Assert.assertTrue(storedOrder.isPresent()); 35 | Assert.assertEquals("order1", storedOrder.get().getId()); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /spring-jms/jms-tx/src/main/java/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /spring-jms/jms-tx/src/main/java/xpadro/spring/jms/model/Notification.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.jms.model; 2 | 3 | import java.io.Serializable; 4 | 5 | public class Notification implements Serializable { 6 | private static final long serialVersionUID = 1L; 7 | 8 | private int id; 9 | private String message; 10 | 11 | 12 | public Notification(int id, String message) { 13 | this.id = id; 14 | this.message = message; 15 | } 16 | 17 | /** 18 | * @return the id 19 | */ 20 | public int getId() { 21 | return id; 22 | } 23 | /** 24 | * @param id the id to set 25 | */ 26 | public void setId(int id) { 27 | this.id = id; 28 | } 29 | /** 30 | * @return the message 31 | */ 32 | public String getMessage() { 33 | return message; 34 | } 35 | /** 36 | * @param message the message to set 37 | */ 38 | public void setMessage(String message) { 39 | this.message = message; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /spring-jms/jms-tx/src/main/java/xpadro/spring/jms/producer/Producer.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.jms.producer; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.jms.core.JmsTemplate; 7 | import org.springframework.stereotype.Component; 8 | 9 | import xpadro.spring.jms.model.Notification; 10 | 11 | /** 12 | * 13 | * @author xpadro 14 | * 15 | */ 16 | @Component("producer") 17 | public class Producer { 18 | private static Logger logger = LoggerFactory.getLogger(Producer.class); 19 | 20 | @Autowired 21 | private JmsTemplate jmsTemplate; 22 | 23 | public void convertAndSendMessage(String destination, Notification notification) { 24 | jmsTemplate.convertAndSend(destination, notification); 25 | logger.info("Sending notification | Id: "+notification.getId()); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /spring-jms/jms-tx/src/main/resources/db/schema.sql: -------------------------------------------------------------------------------- 1 | drop table notifications if exists; 2 | 3 | create table notifications (id integer, message varchar(255)); -------------------------------------------------------------------------------- /spring-jms/jms-tx/src/main/resources/xpadro/spring/jms/config/app-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /spring-jms/jms-tx/src/main/resources/xpadro/spring/jms/config/notx-jms-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /spring-jms/jms-tx/src/main/resources/xpadro/spring/jms/config/tx-jms-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /spring-jms/jms-tx/src/test/java/xpadro/spring/jms/test/TestBaseMessaging.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.jms.test; 2 | 3 | import java.util.Enumeration; 4 | 5 | import javax.jms.JMSException; 6 | import javax.jms.QueueBrowser; 7 | import javax.jms.Session; 8 | 9 | import org.junit.Before; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.jdbc.core.JdbcTemplate; 12 | import org.springframework.jms.core.BrowserCallback; 13 | import org.springframework.jms.core.JmsTemplate; 14 | import org.springframework.test.annotation.DirtiesContext; 15 | import org.springframework.test.context.ContextConfiguration; 16 | 17 | import xpadro.spring.jms.producer.Producer; 18 | 19 | /** 20 | * 21 | * @author xpadro 22 | * 23 | */ 24 | @ContextConfiguration(locations = {"/xpadro/spring/jms/config/app-config.xml"}) 25 | @DirtiesContext 26 | public class TestBaseMessaging { 27 | protected static final String QUEUE_INCOMING = "incoming.queue"; 28 | protected static final String QUEUE_DLQ = "ActiveMQ.DLQ"; 29 | 30 | @Autowired 31 | protected JdbcTemplate jdbcTemplate; 32 | 33 | @Autowired 34 | protected JmsTemplate jmsTemplate; 35 | 36 | @Autowired 37 | protected Producer producer; 38 | 39 | 40 | @Before 41 | public void prepareTest() { 42 | jdbcTemplate.update("delete from Notifications"); 43 | } 44 | 45 | /** 46 | * Returns total notifications saved to the DB 47 | * 48 | * @return 49 | */ 50 | protected int getSavedNotifications() { 51 | return jdbcTemplate.queryForObject("select count(*) from Notifications", Integer.class); 52 | } 53 | 54 | /** 55 | * Returns total messages that are still in the specified queue, waiting for retrieval 56 | * 57 | * @param queueName 58 | * @return 59 | */ 60 | protected int getMessagesInQueue(String queueName) { 61 | return jmsTemplate.browse(queueName, new BrowserCallback() { 62 | @Override 63 | public Integer doInJms(Session session, QueueBrowser browser) throws JMSException { 64 | Enumeration messages = browser.getEnumeration(); 65 | int total = 0; 66 | while (messages.hasMoreElements()) { 67 | messages.nextElement(); 68 | total++; 69 | } 70 | 71 | return total; 72 | } 73 | }); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /webservices/README.md: -------------------------------------------------------------------------------- 1 | # WEB SERVICES 2 | Spring Integration Web Services support 3 | #### Version 3 4 | Examples using Spring Integration 3 release: 5 | 6 | * [spring-ws] - Shows how to implement a web service with the Spring-WS module. The example starts with a sample XML document and uses it to generate the contract. This project uses JAXB2 to generate the Java objects. 7 | * Blog post: http://xpadro.com/2013/09/creating-contract-first-web-services_30.html 8 | 9 | * [ws-retry-adv] - Allows to retry a web service operation using RequestHandlerRetryAdvice. The project spring-ws from this same repository needs to be deployed on the server to test a succesful invocation. Otherwise, it will keep retrying invocations until it reaches a max retries limit. If the limit is reached, it will store the request to a mongoDB database (a mongod instance needs to be running). 10 | * Blog post: http://xpadro.com/2013/12/retry-web-service-operations-with.html 11 | 12 | * [ws-retry] - A custom version of the int-ws-retry-adv project without the support of Spring Retry. This application uses a cron trigger with an inbound adapter to accomplish the same functionality. 13 | 14 | * [ws-timeout] - Example of how to configure a timeout for invoking a web service through a web service outbound gateway. The web service's source code can be found at https://github.com/xpadro/spring-samples/tree/master/spring-ws-courses 15 | * Blog post: http://xpadro.com/2014/04/spring-integration-configure-web.html 16 | 17 | 18 | 19 | [spring-ws]: https://github.com/xpadro/spring-integration/tree/master/webservices/spring-ws 20 | [ws-retry-adv]: https://github.com/xpadro/spring-integration/tree/master/webservices/ws-retry-adv 21 | [ws-retry]: https://github.com/xpadro/spring-integration/tree/master/webservices/ws-retry 22 | [ws-timeout]: https://github.com/xpadro/spring-integration/tree/master/webservices/ws-timeout 23 | -------------------------------------------------------------------------------- /webservices/spring-ws/generate-classes.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /webservices/spring-ws/generate-schema.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /webservices/spring-ws/lib/trang.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xpadro/spring-integration/1e7743f1308b793123a8b7cd2171c20277e3a22f/webservices/spring-ws/lib/trang.jar -------------------------------------------------------------------------------- /webservices/spring-ws/src/main/java/xpadro/spring/ws/OrderEndpoint.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.ws; 2 | 3 | import java.math.BigDecimal; 4 | import java.util.Date; 5 | import java.util.GregorianCalendar; 6 | 7 | import javax.xml.datatype.DatatypeConfigurationException; 8 | import javax.xml.datatype.DatatypeFactory; 9 | import javax.xml.datatype.XMLGregorianCalendar; 10 | 11 | import org.springframework.beans.factory.annotation.Autowired; 12 | import org.springframework.ws.server.endpoint.annotation.Endpoint; 13 | import org.springframework.ws.server.endpoint.annotation.PayloadRoot; 14 | import org.springframework.ws.server.endpoint.annotation.RequestPayload; 15 | import org.springframework.ws.server.endpoint.annotation.ResponsePayload; 16 | 17 | import xpadro.spring.ws.exception.OrderFormatDateException; 18 | import xpadro.spring.ws.model.OrderConfirmation; 19 | import xpadro.spring.ws.service.OrderService; 20 | import xpadro.spring.ws.types.ClientDataRequest; 21 | import xpadro.spring.ws.types.ClientDataResponse; 22 | 23 | @Endpoint 24 | public class OrderEndpoint { 25 | 26 | @Autowired 27 | private OrderService orderService; 28 | 29 | @PayloadRoot(localPart="clientDataRequest", namespace="http://www.xpadro.spring.samples.com/orders") 30 | public @ResponsePayload ClientDataResponse order(@RequestPayload ClientDataRequest orderData) { 31 | OrderConfirmation confirmation = 32 | orderService.order(orderData.getClientId(), orderData.getProductId(), orderData.getQuantity().intValue()); 33 | 34 | ClientDataResponse response = new ClientDataResponse(); 35 | response.setConfirmationId(confirmation.getConfirmationId()); 36 | BigDecimal amount = new BigDecimal(Float.toString(confirmation.getAmount())); 37 | response.setAmount(amount); 38 | response.setOrderDate(convertDate(confirmation.getOrderDate())); 39 | 40 | return response; 41 | } 42 | 43 | private XMLGregorianCalendar convertDate(Date serviceDate) { 44 | GregorianCalendar calendar = new GregorianCalendar(); 45 | calendar.setTime(serviceDate); 46 | try { 47 | return DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar); 48 | } catch (DatatypeConfigurationException e) { 49 | throw new OrderFormatDateException(); 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /webservices/spring-ws/src/main/java/xpadro/spring/ws/exception/ClientNotFoundException.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.ws.exception; 2 | 3 | 4 | public class ClientNotFoundException extends RuntimeException { 5 | private static final long serialVersionUID = 1L; 6 | 7 | public ClientNotFoundException(String message) { 8 | super(message); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /webservices/spring-ws/src/main/java/xpadro/spring/ws/exception/OrderFormatDateException.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.ws.exception; 2 | 3 | 4 | public class OrderFormatDateException extends RuntimeException { 5 | private static final long serialVersionUID = 1L; 6 | 7 | } 8 | -------------------------------------------------------------------------------- /webservices/spring-ws/src/main/java/xpadro/spring/ws/model/OrderConfirmation.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.ws.model; 2 | 3 | import java.util.Date; 4 | 5 | public class OrderConfirmation { 6 | 7 | private String confirmationId; 8 | private float amount; 9 | private Date orderDate; 10 | 11 | 12 | public String getConfirmationId() { 13 | return confirmationId; 14 | } 15 | 16 | public void setConfirmationId(String confirmationId) { 17 | this.confirmationId = confirmationId; 18 | } 19 | 20 | public float getAmount() { 21 | return amount; 22 | } 23 | 24 | public void setAmount(float amount) { 25 | this.amount = amount; 26 | } 27 | 28 | public Date getOrderDate() { 29 | return orderDate; 30 | } 31 | 32 | public void setOrderDate(Date orderDate) { 33 | this.orderDate = orderDate; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /webservices/spring-ws/src/main/java/xpadro/spring/ws/service/OrderService.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.ws.service; 2 | 3 | import xpadro.spring.ws.model.OrderConfirmation; 4 | 5 | public interface OrderService { 6 | 7 | public OrderConfirmation order(String clientId, String productId, int quantity); 8 | } 9 | -------------------------------------------------------------------------------- /webservices/spring-ws/src/main/java/xpadro/spring/ws/service/impl/StubOrderService.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.ws.service.impl; 2 | 3 | import java.util.Calendar; 4 | 5 | import org.springframework.stereotype.Service; 6 | 7 | import xpadro.spring.ws.exception.ClientNotFoundException; 8 | import xpadro.spring.ws.model.OrderConfirmation; 9 | import xpadro.spring.ws.service.OrderService; 10 | 11 | @Service 12 | public class StubOrderService implements OrderService { 13 | private static final String VALID_CLIENT_ID = "123"; 14 | 15 | @Override 16 | public OrderConfirmation order(String clientId, String productId, int quantity) { 17 | if (!VALID_CLIENT_ID.equals(clientId)) { 18 | throw new ClientNotFoundException("Client ["+clientId+"] not found"); 19 | } 20 | 21 | OrderConfirmation response = new OrderConfirmation(); 22 | response.setAmount(55.99f); 23 | response.setConfirmationId("GHKG34L"); 24 | Calendar cal = Calendar.getInstance(); 25 | cal.set(2013, 9, 26); 26 | response.setOrderDate(cal.getTime()); 27 | 28 | return response; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /webservices/spring-ws/src/main/java/xpadro/spring/ws/types/ObjectFactory.java: -------------------------------------------------------------------------------- 1 | // 2 | // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6 3 | // See http://java.sun.com/xml/jaxb 4 | // Any modifications to this file will be lost upon recompilation of the source schema. 5 | // Generated on: 2013.09.27 at 09:59:06 PM CEST 6 | // 7 | 8 | 9 | package xpadro.spring.ws.types; 10 | 11 | import javax.xml.bind.annotation.XmlRegistry; 12 | 13 | 14 | /** 15 | * This object contains factory methods for each 16 | * Java content interface and Java element interface 17 | * generated in the xpadro.spring.ws.types package. 18 | *

An ObjectFactory allows you to programatically 19 | * construct new instances of the Java representation 20 | * for XML content. The Java representation of XML 21 | * content can consist of schema derived interfaces 22 | * and classes representing the binding of schema 23 | * type definitions, element declarations and model 24 | * groups. Factory methods for each of these are 25 | * provided in this class. 26 | * 27 | */ 28 | @XmlRegistry 29 | public class ObjectFactory { 30 | 31 | 32 | /** 33 | * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: xpadro.spring.ws.types 34 | * 35 | */ 36 | public ObjectFactory() { 37 | } 38 | 39 | /** 40 | * Create an instance of {@link ClientDataRequest } 41 | * 42 | */ 43 | public ClientDataRequest createClientDataRequest() { 44 | return new ClientDataRequest(); 45 | } 46 | 47 | /** 48 | * Create an instance of {@link ClientDataResponse } 49 | * 50 | */ 51 | public ClientDataResponse createClientDataResponse() { 52 | return new ClientDataResponse(); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /webservices/spring-ws/src/main/java/xpadro/spring/ws/types/package-info.java: -------------------------------------------------------------------------------- 1 | // 2 | // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6 3 | // See http://java.sun.com/xml/jaxb 4 | // Any modifications to this file will be lost upon recompilation of the source schema. 5 | // Generated on: 2013.09.27 at 09:59:06 PM CEST 6 | // 7 | 8 | @javax.xml.bind.annotation.XmlSchema(namespace = "http://www.xpadro.spring.samples.com/orders") 9 | package xpadro.spring.ws.types; 10 | -------------------------------------------------------------------------------- /webservices/spring-ws/src/main/resources/xpadro/spring/ws/config/root-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /webservices/spring-ws/src/main/resources/xpadro/spring/ws/config/servlet-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /webservices/spring-ws/src/main/webapp/WEB-INF/schemas/samples/client-request.xml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /webservices/spring-ws/src/main/webapp/WEB-INF/schemas/samples/client-response.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | -------------------------------------------------------------------------------- /webservices/spring-ws/src/main/webapp/WEB-INF/schemas/xsd/client-request.xsd: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /webservices/spring-ws/src/main/webapp/WEB-INF/schemas/xsd/client-response.xsd: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /webservices/spring-ws/src/main/webapp/WEB-INF/schemas/xsd/client-service.xsd: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /webservices/spring-ws/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | contextConfigLocation 9 | classpath:xpadro/spring/ws/config/root-config.xml 10 | 11 | 12 | 13 | org.springframework.web.context.ContextLoaderListener 14 | 15 | 16 | 17 | orders 18 | org.springframework.ws.transport.http.MessageDispatcherServlet 19 | 20 | contextConfigLocation 21 | classpath:xpadro/spring/ws/config/servlet-config.xml 22 | 23 | 1 24 | 25 | 26 | 27 | orders 28 | /orders/* 29 | 30 | -------------------------------------------------------------------------------- /webservices/spring-ws/src/test/java/xpadro/spring/ws/test/TestClient.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.ws.test; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | import static org.junit.Assert.assertNotNull; 5 | 6 | import java.math.BigDecimal; 7 | import java.math.BigInteger; 8 | 9 | import org.junit.Test; 10 | import org.junit.runner.RunWith; 11 | import org.springframework.beans.factory.annotation.Autowired; 12 | import org.springframework.test.context.ContextConfiguration; 13 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 14 | import org.springframework.ws.client.core.WebServiceTemplate; 15 | 16 | import xpadro.spring.ws.types.ClientDataRequest; 17 | import xpadro.spring.ws.types.ClientDataResponse; 18 | 19 | @ContextConfiguration("classpath:xpadro/spring/ws/test/config/client-config.xml") 20 | @RunWith(SpringJUnit4ClassRunner.class) 21 | public class TestClient { 22 | @Autowired 23 | WebServiceTemplate wsTemplate; 24 | 25 | @Test 26 | public void invokeOrderService() throws Exception { 27 | ClientDataRequest request = new ClientDataRequest(); 28 | request.setClientId("123"); 29 | request.setProductId("XA-55"); 30 | request.setQuantity(new BigInteger("5", 10)); 31 | 32 | ClientDataResponse response = (ClientDataResponse) wsTemplate.marshalSendAndReceive(request); 33 | 34 | assertNotNull(response); 35 | assertEquals(new BigDecimal("55.99"), response.getAmount()); 36 | assertEquals("GHKG34L", response.getConfirmationId()); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /webservices/spring-ws/src/test/resources/xpadro/spring/ws/test/config/client-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /webservices/spring-ws/src/test/resources/xpadro/spring/ws/test/config/test-server-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /webservices/ws-retry-adv/src/main/java/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /webservices/ws-retry-adv/src/main/java/xpadro/spring/integration/activator/ClientServiceActivator.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.activator; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.integration.Message; 6 | import org.springframework.integration.MessagingException; 7 | import org.springframework.stereotype.Component; 8 | 9 | import xpadro.spring.integration.types.ClientDataResponse; 10 | 11 | @Component("clientServiceActivator") 12 | public class ClientServiceActivator { 13 | private Logger logger = LoggerFactory.getLogger(this.getClass()); 14 | 15 | 16 | /** 17 | * Receives the result from a succesful service invocation. Flow finishes here. 18 | * @param msg 19 | */ 20 | public void handleServiceResult(Message msg) { 21 | logger.info("Service result received: {}", ((ClientDataResponse) msg.getPayload()).getConfirmationId()); 22 | } 23 | 24 | /** 25 | * The service invocation won't succeed. Logs the failed request to the DB and finishes the flow. 26 | * @param exception 27 | */ 28 | public Message handleFailedInvocation(MessagingException exception) { 29 | logger.info("Failed to receive response. Request stored to DB..."); 30 | return exception.getFailedMessage(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /webservices/ws-retry-adv/src/main/java/xpadro/spring/integration/gateway/ClientService.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.gateway; 2 | 3 | import xpadro.spring.integration.types.ClientDataRequest; 4 | 5 | public interface ClientService { 6 | 7 | /** 8 | * Entry to the messaging system. All invocations to this method will be intercepted and sent to the SI "system entry" gateway 9 | * 10 | * @param request 11 | */ 12 | public void invoke(ClientDataRequest request); 13 | } 14 | -------------------------------------------------------------------------------- /webservices/ws-retry-adv/src/main/java/xpadro/spring/integration/interceptor/ServiceClientInterceptor.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.interceptor; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.stereotype.Component; 6 | import org.springframework.ws.client.WebServiceClientException; 7 | import org.springframework.ws.client.support.interceptor.ClientInterceptor; 8 | import org.springframework.ws.context.MessageContext; 9 | 10 | @Component("clientInterceptor") 11 | public class ServiceClientInterceptor implements ClientInterceptor { 12 | private Logger logger = LoggerFactory.getLogger(this.getClass()); 13 | 14 | @Override 15 | public boolean handleFault(MessageContext context) throws WebServiceClientException { 16 | logger.info("handle fault"); 17 | return true; 18 | } 19 | 20 | @Override 21 | public boolean handleRequest(MessageContext context) throws WebServiceClientException { 22 | logger.info("invoking service..."); 23 | 24 | return true; 25 | } 26 | 27 | @Override 28 | public boolean handleResponse(MessageContext context) throws WebServiceClientException { 29 | logger.info("returning from service..."); 30 | 31 | return true; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /webservices/ws-retry-adv/src/main/java/xpadro/spring/integration/types/ObjectFactory.java: -------------------------------------------------------------------------------- 1 | // 2 | // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6 3 | // See http://java.sun.com/xml/jaxb 4 | // Any modifications to this file will be lost upon recompilation of the source schema. 5 | // Generated on: 2013.09.27 at 09:59:06 PM CEST 6 | // 7 | 8 | 9 | package xpadro.spring.integration.types; 10 | 11 | import javax.xml.bind.annotation.XmlRegistry; 12 | 13 | 14 | /** 15 | * This object contains factory methods for each 16 | * Java content interface and Java element interface 17 | * generated in the xpadro.spring.ws.types package. 18 | *

An ObjectFactory allows you to programatically 19 | * construct new instances of the Java representation 20 | * for XML content. The Java representation of XML 21 | * content can consist of schema derived interfaces 22 | * and classes representing the binding of schema 23 | * type definitions, element declarations and model 24 | * groups. Factory methods for each of these are 25 | * provided in this class. 26 | * 27 | */ 28 | @XmlRegistry 29 | public class ObjectFactory { 30 | 31 | 32 | /** 33 | * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: xpadro.spring.ws.types 34 | * 35 | */ 36 | public ObjectFactory() { 37 | } 38 | 39 | /** 40 | * Create an instance of {@link ClientDataRequest } 41 | * 42 | */ 43 | public ClientDataRequest createClientDataRequest() { 44 | return new ClientDataRequest(); 45 | } 46 | 47 | /** 48 | * Create an instance of {@link ClientDataResponse } 49 | * 50 | */ 51 | public ClientDataResponse createClientDataResponse() { 52 | return new ClientDataResponse(); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /webservices/ws-retry-adv/src/main/java/xpadro/spring/integration/types/package-info.java: -------------------------------------------------------------------------------- 1 | // 2 | // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6 3 | // See http://java.sun.com/xml/jaxb 4 | // Any modifications to this file will be lost upon recompilation of the source schema. 5 | // Generated on: 2013.09.27 at 09:59:06 PM CEST 6 | // 7 | 8 | @javax.xml.bind.annotation.XmlSchema(namespace = "http://www.xpadro.spring.samples.com/orders") 9 | package xpadro.spring.integration.types; 10 | -------------------------------------------------------------------------------- /webservices/ws-retry-adv/src/test/java/xpadro/spring/integration/TestInvocation.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration; 2 | 3 | import java.math.BigInteger; 4 | import java.util.concurrent.ExecutionException; 5 | 6 | import org.junit.Test; 7 | import org.junit.runner.RunWith; 8 | import org.slf4j.Logger; 9 | import org.slf4j.LoggerFactory; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.test.context.ContextConfiguration; 12 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 13 | 14 | import xpadro.spring.integration.gateway.ClientService; 15 | import xpadro.spring.integration.types.ClientDataRequest; 16 | 17 | @ContextConfiguration({"classpath:xpadro/spring/integration/config/int-config.xml", 18 | "classpath:xpadro/spring/integration/config/mongodb-config.xml"}) 19 | @RunWith(SpringJUnit4ClassRunner.class) 20 | public class TestInvocation { 21 | private Logger logger = LoggerFactory.getLogger(this.getClass()); 22 | 23 | @Autowired 24 | private ClientService service; 25 | 26 | @Test 27 | public void testInvocation() throws InterruptedException, ExecutionException { 28 | logger.info("Initiating service request..."); 29 | 30 | ClientDataRequest request = new ClientDataRequest(); 31 | request.setClientId("123"); 32 | request.setProductId("XA-55"); 33 | request.setQuantity(new BigInteger("5")); 34 | 35 | service.invoke(request); 36 | logger.info("Doing other stuff..."); 37 | 38 | Thread.sleep(60000); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /webservices/ws-retry-adv/src/test/resources/xpadro/spring/integration/config/mongodb-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /webservices/ws-retry/src/main/java/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /webservices/ws-retry/src/main/java/xpadro/spring/integration/data/RequestData.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.data; 2 | 3 | import org.springframework.stereotype.Component; 4 | 5 | import xpadro.spring.integration.types.ClientDataRequest; 6 | 7 | /** 8 | * Stores the request that will be retrieved by the service activator when retrying the service invocation 9 | * 10 | */ 11 | @Component("requestData") 12 | public class RequestData { 13 | 14 | private ClientDataRequest request; 15 | 16 | public ClientDataRequest getRequest() { 17 | return request; 18 | } 19 | 20 | public void setRequest(ClientDataRequest request) { 21 | this.request = request; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /webservices/ws-retry/src/main/java/xpadro/spring/integration/gateway/ClientService.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.gateway; 2 | 3 | import xpadro.spring.integration.types.ClientDataRequest; 4 | 5 | public interface ClientService { 6 | 7 | /** 8 | * Entry to the messaging system. All invocations to this method will be intercepted and sent to the SI "system entry" gateway 9 | * 10 | * @param request 11 | */ 12 | public void invoke(ClientDataRequest request); 13 | } 14 | -------------------------------------------------------------------------------- /webservices/ws-retry/src/main/java/xpadro/spring/integration/router/ServiceResultRouter.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.router; 2 | 3 | import java.util.concurrent.locks.ReentrantLock; 4 | 5 | import org.slf4j.Logger; 6 | import org.slf4j.LoggerFactory; 7 | import org.springframework.integration.Message; 8 | import org.springframework.stereotype.Component; 9 | 10 | @Component("resultRouter") 11 | public class ServiceResultRouter { 12 | private final ReentrantLock lock = new ReentrantLock(); 13 | 14 | private Logger logger = LoggerFactory.getLogger(this.getClass()); 15 | 16 | private int maxRetries = 3; 17 | private int currentRetries; 18 | 19 | private static final String FAIL_CHANNEL = "failedChannel"; 20 | private static final String RETRY_CHANNEL = "retryChannel"; 21 | 22 | 23 | /** 24 | * The service invocation failed. If maxRetries aren't reached, it will send the request to the retry channel. Otherwise, it will send the failed request 25 | * to the failed channel in order to log the request and finish the flow. 26 | * @param msg 27 | * @return 28 | */ 29 | public String handleServiceError(Message msg) { 30 | lock.lock(); 31 | try { 32 | logger.info("Handling service failure"); 33 | if (maxRetries > 0) { 34 | currentRetries++; 35 | if (currentRetries > maxRetries) { 36 | logger.info("Max retries [{}] reached", maxRetries); 37 | return FAIL_CHANNEL; 38 | } 39 | } 40 | 41 | logger.info("Retry number {} of {}", currentRetries, maxRetries); 42 | return RETRY_CHANNEL; 43 | } finally { 44 | lock.unlock(); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /webservices/ws-retry/src/main/java/xpadro/spring/integration/trigger/ServiceRetryTrigger.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.trigger; 2 | 3 | import java.util.Date; 4 | 5 | import org.springframework.scheduling.Trigger; 6 | import org.springframework.scheduling.TriggerContext; 7 | 8 | public class ServiceRetryTrigger implements Trigger { 9 | private long rate; 10 | 11 | 12 | public long getRate() { 13 | return rate; 14 | } 15 | 16 | public void setRate(long rate) { 17 | this.rate = rate; 18 | } 19 | 20 | 21 | @Override 22 | public Date nextExecutionTime(TriggerContext triggerContext) { 23 | if (triggerContext.lastScheduledExecutionTime() == null) { 24 | return new Date(System.currentTimeMillis()); 25 | } 26 | 27 | return new Date(triggerContext.lastScheduledExecutionTime().getTime() + getRate()); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /webservices/ws-retry/src/main/java/xpadro/spring/integration/types/ObjectFactory.java: -------------------------------------------------------------------------------- 1 | // 2 | // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6 3 | // See http://java.sun.com/xml/jaxb 4 | // Any modifications to this file will be lost upon recompilation of the source schema. 5 | // Generated on: 2013.09.27 at 09:59:06 PM CEST 6 | // 7 | 8 | 9 | package xpadro.spring.integration.types; 10 | 11 | import javax.xml.bind.annotation.XmlRegistry; 12 | 13 | 14 | /** 15 | * This object contains factory methods for each 16 | * Java content interface and Java element interface 17 | * generated in the xpadro.spring.ws.types package. 18 | *

An ObjectFactory allows you to programatically 19 | * construct new instances of the Java representation 20 | * for XML content. The Java representation of XML 21 | * content can consist of schema derived interfaces 22 | * and classes representing the binding of schema 23 | * type definitions, element declarations and model 24 | * groups. Factory methods for each of these are 25 | * provided in this class. 26 | * 27 | */ 28 | @XmlRegistry 29 | public class ObjectFactory { 30 | 31 | 32 | /** 33 | * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: xpadro.spring.ws.types 34 | * 35 | */ 36 | public ObjectFactory() { 37 | } 38 | 39 | /** 40 | * Create an instance of {@link ClientDataRequest } 41 | * 42 | */ 43 | public ClientDataRequest createClientDataRequest() { 44 | return new ClientDataRequest(); 45 | } 46 | 47 | /** 48 | * Create an instance of {@link ClientDataResponse } 49 | * 50 | */ 51 | public ClientDataResponse createClientDataResponse() { 52 | return new ClientDataResponse(); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /webservices/ws-retry/src/main/java/xpadro/spring/integration/types/package-info.java: -------------------------------------------------------------------------------- 1 | // 2 | // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6 3 | // See http://java.sun.com/xml/jaxb 4 | // Any modifications to this file will be lost upon recompilation of the source schema. 5 | // Generated on: 2013.09.27 at 09:59:06 PM CEST 6 | // 7 | 8 | @javax.xml.bind.annotation.XmlSchema(namespace = "http://www.xpadro.spring.samples.com/orders") 9 | package xpadro.spring.integration.types; 10 | -------------------------------------------------------------------------------- /webservices/ws-retry/src/test/java/xpadro/spring/integration/TestInvocation.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration; 2 | 3 | import java.math.BigInteger; 4 | import java.util.concurrent.ExecutionException; 5 | 6 | import org.junit.Test; 7 | import org.junit.runner.RunWith; 8 | import org.slf4j.Logger; 9 | import org.slf4j.LoggerFactory; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.test.context.ContextConfiguration; 12 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 13 | 14 | import xpadro.spring.integration.data.RequestData; 15 | import xpadro.spring.integration.gateway.ClientService; 16 | import xpadro.spring.integration.types.ClientDataRequest; 17 | 18 | @ContextConfiguration({"classpath:xpadro/spring/integration/config/int-config.xml", 19 | "classpath:xpadro/spring/integration/config/mongodb-config.xml"}) 20 | @RunWith(SpringJUnit4ClassRunner.class) 21 | public class TestInvocation { 22 | private Logger logger = LoggerFactory.getLogger(this.getClass()); 23 | 24 | @Autowired 25 | private ClientService service; 26 | 27 | @Autowired 28 | private RequestData requestData; 29 | 30 | 31 | @Test 32 | public void testInvocation() throws InterruptedException, ExecutionException { 33 | logger.info("Initiating service request..."); 34 | 35 | ClientDataRequest request = new ClientDataRequest(); 36 | request.setClientId("123"); 37 | request.setProductId("XA-55"); 38 | request.setQuantity(new BigInteger("5")); 39 | requestData.setRequest(request); 40 | 41 | service.invoke(request); 42 | logger.info("Doing other stuff..."); 43 | 44 | Thread.sleep(60000); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /webservices/ws-retry/src/test/resources/xpadro/spring/integration/config/mongodb-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 19 | -------------------------------------------------------------------------------- /webservices/ws-timeout/src/main/java/xpadro/spring/integration/ws/gateway/CourseService.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.ws.gateway; 2 | 3 | import org.springframework.integration.annotation.Gateway; 4 | 5 | import xpadro.spring.integration.ws.types.GetCourseRequest; 6 | import xpadro.spring.integration.ws.types.GetCourseResponse; 7 | 8 | public interface CourseService { 9 | 10 | @Gateway 11 | GetCourseResponse getCourse(GetCourseRequest request); 12 | } 13 | -------------------------------------------------------------------------------- /webservices/ws-timeout/src/main/java/xpadro/spring/integration/ws/types/GetCourseListRequest.java: -------------------------------------------------------------------------------- 1 | // 2 | // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.4-2 3 | // See http://java.sun.com/xml/jaxb 4 | // Any modifications to this file will be lost upon recompilation of the source schema. 5 | // Generated on: 2014.04.24 at 04:39:37 PM CEST 6 | // 7 | 8 | 9 | package xpadro.spring.integration.ws.types; 10 | 11 | import javax.xml.bind.annotation.XmlAccessType; 12 | import javax.xml.bind.annotation.XmlAccessorType; 13 | import javax.xml.bind.annotation.XmlRootElement; 14 | import javax.xml.bind.annotation.XmlType; 15 | 16 | 17 | /** 18 | *

Java class for anonymous complex type. 19 | * 20 | *

The following schema fragment specifies the expected content contained within this class. 21 | * 22 | *

23 |  * <complexType>
24 |  *   <complexContent>
25 |  *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
26 |  *     </restriction>
27 |  *   </complexContent>
28 |  * </complexType>
29 |  * 
30 | * 31 | * 32 | */ 33 | @XmlAccessorType(XmlAccessType.FIELD) 34 | @XmlType(name = "") 35 | @XmlRootElement(name = "getCourseListRequest") 36 | public class GetCourseListRequest { 37 | 38 | 39 | } 40 | -------------------------------------------------------------------------------- /webservices/ws-timeout/src/main/java/xpadro/spring/integration/ws/types/GetCourseRequest.java: -------------------------------------------------------------------------------- 1 | // 2 | // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.4-2 3 | // See http://java.sun.com/xml/jaxb 4 | // Any modifications to this file will be lost upon recompilation of the source schema. 5 | // Generated on: 2014.04.24 at 04:39:37 PM CEST 6 | // 7 | 8 | 9 | package xpadro.spring.integration.ws.types; 10 | 11 | import javax.xml.bind.annotation.XmlAccessType; 12 | import javax.xml.bind.annotation.XmlAccessorType; 13 | import javax.xml.bind.annotation.XmlAttribute; 14 | import javax.xml.bind.annotation.XmlRootElement; 15 | import javax.xml.bind.annotation.XmlType; 16 | 17 | 18 | /** 19 | *

Java class for anonymous complex type. 20 | * 21 | *

The following schema fragment specifies the expected content contained within this class. 22 | * 23 | *

24 |  * <complexType>
25 |  *   <complexContent>
26 |  *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
27 |  *       <attribute name="courseId" use="required" type="{http://www.w3.org/2001/XMLSchema}string" />
28 |  *     </restriction>
29 |  *   </complexContent>
30 |  * </complexType>
31 |  * 
32 | * 33 | * 34 | */ 35 | @XmlAccessorType(XmlAccessType.FIELD) 36 | @XmlType(name = "") 37 | @XmlRootElement(name = "getCourseRequest") 38 | public class GetCourseRequest { 39 | 40 | @XmlAttribute(name = "courseId", required = true) 41 | protected String courseId; 42 | 43 | /** 44 | * Gets the value of the courseId property. 45 | * 46 | * @return 47 | * possible object is 48 | * {@link String } 49 | * 50 | */ 51 | public String getCourseId() { 52 | return courseId; 53 | } 54 | 55 | /** 56 | * Sets the value of the courseId property. 57 | * 58 | * @param value 59 | * allowed object is 60 | * {@link String } 61 | * 62 | */ 63 | public void setCourseId(String value) { 64 | this.courseId = value; 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /webservices/ws-timeout/src/main/java/xpadro/spring/integration/ws/types/package-info.java: -------------------------------------------------------------------------------- 1 | // 2 | // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.4-2 3 | // See http://java.sun.com/xml/jaxb 4 | // Any modifications to this file will be lost upon recompilation of the source schema. 5 | // Generated on: 2014.04.24 at 04:39:37 PM CEST 6 | // 7 | 8 | @javax.xml.bind.annotation.XmlSchema(namespace = "http://www.xpadro.spring.samples.com/courses", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED) 9 | package xpadro.spring.integration.ws.types; 10 | -------------------------------------------------------------------------------- /webservices/ws-timeout/src/main/resources/props/service.properties: -------------------------------------------------------------------------------- 1 | timeout.connection=2000 2 | timeout.read=2000 -------------------------------------------------------------------------------- /webservices/ws-timeout/src/main/resources/xpadro/spring/integration/ws/config/int-course-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 14 | 15 | 16 | 17 | 18 | 19 | 21 | 22 | 23 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /webservices/ws-timeout/src/test/java/xpadro/spring/integration/ws/test/TestIntegrationApp.java: -------------------------------------------------------------------------------- 1 | package xpadro.spring.integration.ws.test; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | import static org.junit.Assert.assertNotNull; 5 | import static org.junit.Assert.assertNull; 6 | import static org.junit.Assert.assertTrue; 7 | 8 | import java.net.SocketTimeoutException; 9 | 10 | import org.junit.Test; 11 | import org.junit.runner.RunWith; 12 | import org.springframework.beans.factory.annotation.Autowired; 13 | import org.springframework.test.context.ContextConfiguration; 14 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 15 | import org.springframework.ws.client.WebServiceIOException; 16 | 17 | import xpadro.spring.integration.ws.gateway.CourseService; 18 | import xpadro.spring.integration.ws.types.GetCourseRequest; 19 | import xpadro.spring.integration.ws.types.GetCourseResponse; 20 | 21 | @ContextConfiguration(locations = {"/xpadro/spring/integration/ws/config/int-course-config.xml"}) 22 | @RunWith(SpringJUnit4ClassRunner.class) 23 | public class TestIntegrationApp { 24 | 25 | @Autowired 26 | private CourseService service; 27 | 28 | @Test 29 | public void invokeNormalOperation() { 30 | GetCourseRequest request = new GetCourseRequest(); 31 | request.setCourseId("BC-45"); 32 | 33 | GetCourseResponse response = service.getCourse(request); 34 | assertNotNull(response); 35 | assertEquals("Introduction to Java", response.getName()); 36 | } 37 | 38 | @Test 39 | public void invokeTimeoutOperation() { 40 | try { 41 | GetCourseRequest request = new GetCourseRequest(); 42 | request.setCourseId("DF-21"); 43 | 44 | GetCourseResponse response = service.getCourse(request); 45 | assertNull(response); 46 | } catch (WebServiceIOException e) { 47 | assertTrue(e.getCause() instanceof SocketTimeoutException); 48 | } 49 | } 50 | } 51 | --------------------------------------------------------------------------------