{"id":815,"date":"2015-04-08T11:02:49","date_gmt":"2015-04-08T15:02:49","guid":{"rendered":"http:\/\/springframework.guru\/?p=815"},"modified":"2019-06-30T13:36:23","modified_gmt":"2019-06-30T17:36:23","slug":"hello-world-using-spring-integration","status":"publish","type":"post","link":"https:\/\/springframework.guru\/hello-world-using-spring-integration\/","title":{"rendered":"Hello World Using Spring Integration"},"content":{"rendered":"<p><a title=\"Spring Integration Home\" href=\"http:\/\/projects.spring.io\/spring-integration\/\" target=\"_blank\" rel=\"noopener noreferrer\">Spring Integration<\/a> is a very powerful module in the Spring Framework. It was originally inspired by pivotal book in computer science called <a title=\"Enterprise Integration Patterns\" href=\"http:\/\/www.amazon.com\/gp\/product\/0321200683\/ref=as_li_tl?ie=UTF8&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0321200683&amp;linkCode=as2&amp;tag=triatcraft-20&amp;linkId=XFVZDI446PZJCXUU&quot;\" target=\"_blank\" rel=\"noopener noreferrer\">Enterprise Integration Patterns<\/a> written by Gregor Hohpe and Bobby Woolf. While the <a title=\"Gang of Four Design Patterns\" href=\"http:\/\/springframework.guru\/gang-of-four-design-patterns\/\" target=\"_blank\" rel=\"noopener noreferrer\">Gang of Four<\/a> focused their work on software design patterns, Enterprise Integration Patterns is focused on how disparate systems communicate with each other. As the industry has moved to highly scalable systems adopting cloud computing paradigms, the principles set forth in Enterprise Integration Patterns are highly relevant to Java programming.<\/p>\n<p>Similar to the GoF focus, Spring Core largely focused on development within a single application or JVM environment.\u00a0\u00a0Spring Integration builds upon the core principles of Spring Core to extend the Spring programming model to a message driven programming model. Abstracted from the developer when making a method call is the implementation details. Does it really matter if the method you are calling is a simple method call, a web service, a JMS Message, or making a request to the database? No, it should not. And more importantly, the implementation details should not leak back into your code.<\/p>\n<p>In this example, I&#8217;ll step through the use of a Spring Integration Message Gateway for a classic &#8220;<a title=\"Hello World With Spring 4\" href=\"http:\/\/springframework.guru\/hello-world-with-spring-4\/\">Hello World<\/a>&#8221; programming example. I&#8217;ll show you how to use a Spring Integration Messaging Gateway for a simple send message, a request\/response example, and an async request response example.<\/p>\n<h2>Spring Integration Hello World Code Example<\/h2>\n<h3>Hello World Service Bean<\/h3>\n<p>For our example, we&#8217;re going to use a very simple service bean to either say hello or get a hello message. In a real world example, this code could be a direct method call, behind a web service, or perhaps its a message driven bean off a AQMP queue.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\">package guru.springframework.hello.world.si.service;\r\n\r\nimport org.springframework.stereotype.Service;\r\n\r\n@Service\r\npublic class HelloService {\r\n    public void sayHello(String name) {\r\n        System.out.println(\"Hello \" + name + \"!!\");\r\n    }\r\n\r\n    public String getHelloMessage(String name){\r\n        return (\"Hello \" + name + \"!!\");\r\n    }\r\n}\r\n<\/pre>\n<h3>Spring Integration Gateway<\/h3>\n<p>The next component we need to setup is an interface class, which we will use for the Spring Integration Gateways. When coding to use Spring Integration, messaging gateways are a very convenient programming option to use.\u00a0They have a very minimal impact on your application code. As the developer, you only need to create the interface specification. Spring Integration will provide the actual implementation and inject it into your class at run time. There is one small bit of leakage into your application code with the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">@Gateway<\/code>\u00a0\u00a0annotation. I could have coded this example without the annotations, but I find them very convenient to use. This annotation directs Spring Integration which messaging channel to use for the method call of the interface.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\">package guru.springframework.hello.world.si.gateways;\r\n\r\nimport org.springframework.integration.annotation.Gateway;\r\n\r\nimport java.util.concurrent.Future;\r\n\r\npublic interface HelloWorldGateway {\r\n    @Gateway(requestChannel = \"say.hello.channel\")\r\n    void sayHello(String name);\r\n\r\n    @Gateway(requestChannel = \"get.hello.channel\")\r\n    String getHelloMessage(String name);\r\n\r\n    @Gateway(requestChannel = \"get.hello.channel\")\r\n    Future getHelloMessageAsync(String name);\r\n}\r\n<\/pre>\n<h3>Spring Integration Gateway Configuration<\/h3>\n<p>The messaging gateway needs to be defined in your Spring Integration configuration. Spring Integration will not create the gateway bean without this configuration line.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\">&lt;gateway id=\"helloWorldGateway\" service-interface=\"guru.springframework.hello.world.si.gateways.HelloWorldGateway\"\/&gt;<\/pre>\n<h3>Spring Integration Channel<\/h3>\n<p>Spring Integration Channels are a core concept to Spring Integration. They are used to decouple message producers (our gateway) from message consumers (our hello world bean). This abstraction is important because the channel could be a direct method call, or a JMS queue. The important take away is the decoupling. Neither the message producer nor the message consumer is aware of details about the implementation of the messaging channel.<\/p>\n<p>In XML\u00a0configuration snippet below, I&#8217;ve defined two messaging channels. Spring Integration has a number of options for defining <a title=\"Spring Integration Messaging Channels\" href=\"http:\/\/docs.spring.io\/spring-integration\/reference\/html\/messaging-channels-section.html\" target=\"_blank\" rel=\"noopener noreferrer\">messaging channels<\/a>. The default channel is a direct channel, which is perfect for our hello world example.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\">    &lt;channel id=\"say.hello.channel\"\/&gt;\r\n\r\n    &lt;channel id=\"get.hello.channel\"\/&gt;<\/pre>\n<h3>Spring Integration Service Activator<\/h3>\n<p>Spring Integration has a concept of Service Activators. This is basically a way to configure a message consumer on a channel. This is completely decoupled from the gateway. Its important to remember the service activator is associated with a channel, and not a gateway or a gateway method. When you&#8217;re new to Spring Integration, it can be easy to lose site of this important distinction.<\/p>\n<p>In this XML configuration snippet, I&#8217;ve defined two service activators.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\">    &lt;service-activator id=\"sayHello\" input-channel=\"say.hello.channel\" ref=\"helloService\" method=\"sayHello\"\/&gt;\r\n\r\n    &lt;service-activator id=\"getHello\" input-channel=\"get.hello.channel\" ref=\"helloService\" method=\"getHelloMessage\"\/&gt;<\/pre>\n<h3>Spring Integration XML Configuration<\/h3>\n<p>The complete XML configuration used in our hello world example follows below. You&#8217;ll see that I&#8217;m using a <a title=\"Creating Spring Beans\" href=\"http:\/\/springframework.guru\/creating-spring-beans\/\">component scan<\/a> to create the Hello World Service bean in the Spring context.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\">&lt;?xml\u00a0version=\"1.0\"\u00a0encoding=\"UTF-8\"?&gt;\r\n&lt;beans:beans\u00a0xmlns=\"http:\/\/www.springframework.org\/schema\/integration\"\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0xmlns:beans=\"http:\/\/www.springframework.org\/schema\/beans\"\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0xmlns:context=\"http:\/\/www.springframework.org\/schema\/context\"\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0xsi:schemaLocation=\"http:\/\/www.springframework.org\/schema\/beans\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0http:\/\/www.springframework.org\/schema\/beans\/spring-beans.xsd\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0http:\/\/www.springframework.org\/schema\/integration\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0http:\/\/www.springframework.org\/schema\/integration\/spring-integration.xsd\u00a0http:\/\/www.springframework.org\/schema\/context\u00a0http:\/\/www.springframework.org\/schema\/context\/spring-context.xsd\"&gt;\r\n\r\n\u00a0\u00a0\u00a0\u00a0&lt;context:component-scan\u00a0base-package=\"guru.springframework.hello.world.si.service\"\/&gt;\r\n\r\n\u00a0\u00a0\u00a0\u00a0&lt;gateway\u00a0id=\"helloWorldGateway\"\u00a0service-interface=\"guru.springframework.hello.world.si.gateways.HelloWorldGateway\"\/&gt;\r\n\r\n\u00a0\u00a0\u00a0\u00a0&lt;channel\u00a0id=\"say.hello.channel\"\/&gt;\r\n\r\n\u00a0\u00a0\u00a0\u00a0&lt;service-activator\u00a0id=\"sayHello\"\u00a0input-channel=\"say.hello.channel\"\u00a0ref=\"helloService\"\u00a0method=\"sayHello\"\/&gt;\r\n\r\n\u00a0\u00a0\u00a0\u00a0&lt;channel\u00a0id=\"get.hello.channel\"\/&gt;\r\n\r\n\u00a0\u00a0\u00a0\u00a0&lt;service-activator\u00a0id=\"getHello\"\u00a0input-channel=\"get.hello.channel\"\u00a0ref=\"helloService\"\u00a0method=\"getHelloMessage\"\/&gt;\r\n\r\n&lt;\/beans:beans&gt;\r\n\r\n<\/pre>\n<h2>Running the Spring Integration Hello World Example<\/h2>\n<p>I&#8217;ve setup two examples to run our Spring Integration Hello World example. One using Spring Boot, and the second using JUnit.<\/p>\n<h3>Spring Boot Application<\/h3>\n<p>In this example, I&#8217;m using <a title=\"Getting Started With Spring Boot\" href=\"http:\/\/springframework.guru\/getting-started-with-spring-boot\/\">Spring Boot<\/a> to bring up the Spring Context. Since I placed the Spring Integration configuration in a XML configuration file, I need to use the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">@ImportResource<\/code>\u00a0\u00a0annotation to tell Spring Boot to import the XML configuration file.\u00a0 In the code, I ask the Spring Context for an instance of the gateway bean. Then I call our three methods. The first is a simple send message with no return value. Our messaging service bean will write the hello message to the console. In the second example, I&#8217;m getting a message back from the Hello World Service bean.\u00a0 The final example is a little more interesting. In this case, I&#8217;ve setup an asynchronous call. By specifying a Future in the return type, Spring Integration will automatically make a asynchronous call. This is an ideal technique to use when you have a longer running call (such as a remote web service), and you can do other work while the call is being made.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\">package guru.springframework.hello.world.si;\r\n\r\nimport guru.springframework.hello.world.si.gateways.HelloWorldGateway;\r\nimport org.springframework.boot.SpringApplication;\r\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\r\nimport org.springframework.context.ApplicationContext;\r\nimport org.springframework.context.annotation.ImportResource;\r\n\r\nimport java.util.concurrent.ExecutionException;\r\nimport java.util.concurrent.Future;\r\n\r\n@SpringBootApplication\r\n@ImportResource(\"classpath*:\/spring\/si-config.xml\")\r\npublic class HelloWorldSiApplication {\r\n\r\n    public static void main(String[] args) {\r\n        ApplicationContext ctx = SpringApplication.run(HelloWorldSiApplication.class, args);\r\n        HelloWorldGateway gateway = (HelloWorldGateway) ctx.getBean(\"helloWorldGateway\");\r\n        gateway.sayHello(\"John\");\r\n\r\n        String message = gateway.getHelloMessage(\"John (get message)\");\r\n        System.out.println(message);\r\n\r\n        Future helloFuture = gateway.getHelloMessageAsync(\"John (Async!)\");\r\n\r\n        try {\r\n            String helloFutureMsg = helloFuture.get();\r\n            System.out.println(helloFutureMsg);\r\n        } catch (InterruptedException e) {\r\n            e.printStackTrace();\r\n        } catch (ExecutionException e) {\r\n            e.printStackTrace();\r\n        }\r\n\r\n    }\r\n}\r\n<\/pre>\n<p>When you execute the above code, you will receive the following output in the console.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">  .   ____          _            __ _ _\r\n \/\\\\ \/ ___'_ __ _ _(_)_ __  __ _ \\ \\ \\ \\\r\n( ( )\\___ | '_ | '_| | '_ \\\/ _` | \\ \\ \\ \\\r\n \\\\\/  ___)| |_)| | | | | || (_| |  ) ) ) )\r\n  '  |____| .__|_| |_|_| |_\\__, | \/ \/ \/ \/\r\n =========|_|==============|___\/=\/_\/_\/_\/\r\n :: Spring Boot ::        (v1.2.3.RELEASE)\r\n\r\n2015-04-08 10:35:29.665  INFO 11513 --- [           main] g.s.h.world.si.HelloWorldSiApplication   : Starting HelloWorldSiApplication on Johns-MacBook-Pro.local with PID 11513 (\/Users\/jt\/src\/springframework.guru\/blog\/hello-world-si\/target\/classes started by jt in \/Users\/jt\/src\/springframework.guru\/blog\/hello-world-si)\r\n2015-04-08 10:35:29.773  INFO 11513 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@8646db9: startup date [Wed Apr 08 10:35:29 EDT 2015]; root of context hierarchy\r\n2015-04-08 10:35:30.827  INFO 11513 --- [           main] o.s.b.f.xml.XmlBeanDefinitionReader      : Loading XML bean definitions from URL [file:\/Users\/jt\/src\/springframework.guru\/blog\/hello-world-si\/target\/classes\/spring\/si-config.xml]\r\n2015-04-08 10:35:31.104  INFO 11513 --- [           main] o.s.b.f.config.PropertiesFactoryBean     : Loading properties file from URL [jar:file:\/Users\/jt\/.m2\/repository\/org\/springframework\/integration\/spring-integration-core\/4.1.2.RELEASE\/spring-integration-core-4.1.2.RELEASE.jar!\/META-INF\/spring.integration.default.properties]\r\n2015-04-08 10:35:31.113  INFO 11513 --- [           main] o.s.i.config.IntegrationRegistrar        : No bean named 'integrationHeaderChannelRegistry' has been explicitly defined. Therefore, a default DefaultHeaderChannelRegistry will be created.\r\n2015-04-08 10:35:31.367  INFO 11513 --- [           main] faultConfiguringBeanFactoryPostProcessor : No bean named 'errorChannel' has been explicitly defined. Therefore, a default PublishSubscribeChannel will be created.\r\n2015-04-08 10:35:31.375  INFO 11513 --- [           main] faultConfiguringBeanFactoryPostProcessor : No bean named 'taskScheduler' has been explicitly defined. Therefore, a default ThreadPoolTaskScheduler will be created.\r\n2015-04-08 10:35:31.594  INFO 11513 --- [           main] o.s.b.f.config.PropertiesFactoryBean     : Loading properties file from URL [jar:file:\/Users\/jt\/.m2\/repository\/org\/springframework\/integration\/spring-integration-core\/4.1.2.RELEASE\/spring-integration-core-4.1.2.RELEASE.jar!\/META-INF\/spring.integration.default.properties]\r\n2015-04-08 10:35:31.595  INFO 11513 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'integrationGlobalProperties' of type [class org.springframework.beans.factory.config.PropertiesFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)\r\n2015-04-08 10:35:31.595  INFO 11513 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'integrationGlobalProperties' of type [class java.util.Properties] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)\r\n2015-04-08 10:35:31.598  INFO 11513 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'messageBuilderFactory' of type [class org.springframework.integration.support.DefaultMessageBuilderFactory] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)\r\n2015-04-08 10:35:31.688  INFO 11513 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean '(inner bean)#d771cc9' of type [class org.springframework.integration.channel.MessagePublishingErrorHandler] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)\r\n2015-04-08 10:35:31.688  INFO 11513 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService  'taskScheduler'\r\n2015-04-08 10:35:31.690  INFO 11513 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'taskScheduler' of type [class org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)\r\n2015-04-08 10:35:31.690  INFO 11513 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'integrationHeaderChannelRegistry' of type [class org.springframework.integration.channel.DefaultHeaderChannelRegistry] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)\r\n2015-04-08 10:35:31.846  INFO 11513 --- [           main] ProxyFactoryBean$MethodInvocationGateway : started helloWorldGateway\r\n2015-04-08 10:35:31.846  INFO 11513 --- [           main] ProxyFactoryBean$MethodInvocationGateway : started helloWorldGateway\r\n2015-04-08 10:35:31.846  INFO 11513 --- [           main] ProxyFactoryBean$MethodInvocationGateway : started helloWorldGateway\r\n2015-04-08 10:35:31.846  INFO 11513 --- [           main] o.s.i.gateway.GatewayProxyFactoryBean    : started helloWorldGateway\r\n2015-04-08 10:35:32.235  INFO 11513 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup\r\n2015-04-08 10:35:32.244  INFO 11513 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase -2147483648\r\n2015-04-08 10:35:32.246  INFO 11513 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 0\r\n2015-04-08 10:35:32.247  INFO 11513 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : Adding {service-activator:sayHello} as a subscriber to the 'say.hello.channel' channel\r\n2015-04-08 10:35:32.247  INFO 11513 --- [           main] o.s.integration.channel.DirectChannel    : Channel 'application.say.hello.channel' has 1 subscriber(s).\r\n2015-04-08 10:35:32.247  INFO 11513 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : started sayHello\r\n2015-04-08 10:35:32.248  INFO 11513 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : Adding {service-activator:getHello} as a subscriber to the 'get.hello.channel' channel\r\n2015-04-08 10:35:32.248  INFO 11513 --- [           main] o.s.integration.channel.DirectChannel    : Channel 'application.get.hello.channel' has 1 subscriber(s).\r\n2015-04-08 10:35:32.248  INFO 11513 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : started getHello\r\n2015-04-08 10:35:32.248  INFO 11513 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : Adding {logging-channel-adapter:_org.springframework.integration.errorLogger} as a subscriber to the 'errorChannel' channel\r\n2015-04-08 10:35:32.248  INFO 11513 --- [           main] o.s.i.channel.PublishSubscribeChannel    : Channel 'application.errorChannel' has 1 subscriber(s).\r\n2015-04-08 10:35:32.248  INFO 11513 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : started _org.springframework.integration.errorLogger\r\n2015-04-08 10:35:32.256  INFO 11513 --- [           main] g.s.h.world.si.HelloWorldSiApplication   : Started HelloWorldSiApplication in 3.063 seconds (JVM running for 3.829)\r\nHello John!!\r\nHello John (get message)!!\r\nHello John (Async!)!!\r\n2015-04-08 10:35:32.284  INFO 11513 --- [       Thread-1] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@8646db9: startup date [Wed Apr 08 10:35:29 EDT 2015]; root of context hierarchy\r\n2015-04-08 10:35:32.285  INFO 11513 --- [       Thread-1] o.s.c.support.DefaultLifecycleProcessor  : Stopping beans in phase 0\r\n2015-04-08 10:35:32.285  INFO 11513 --- [       Thread-1] ProxyFactoryBean$MethodInvocationGateway : stopped helloWorldGateway\r\n2015-04-08 10:35:32.286  INFO 11513 --- [       Thread-1] ProxyFactoryBean$MethodInvocationGateway : stopped helloWorldGateway\r\n2015-04-08 10:35:32.286  INFO 11513 --- [       Thread-1] ProxyFactoryBean$MethodInvocationGateway : stopped helloWorldGateway\r\n2015-04-08 10:35:32.286  INFO 11513 --- [       Thread-1] o.s.i.gateway.GatewayProxyFactoryBean    : stopped helloWorldGateway\r\n2015-04-08 10:35:32.286  INFO 11513 --- [       Thread-1] o.s.i.endpoint.EventDrivenConsumer       : Removing {service-activator:sayHello} as a subscriber to the 'say.hello.channel' channel\r\n2015-04-08 10:35:32.286  INFO 11513 --- [       Thread-1] o.s.integration.channel.DirectChannel    : Channel 'application.say.hello.channel' has 0 subscriber(s).\r\n2015-04-08 10:35:32.286  INFO 11513 --- [       Thread-1] o.s.i.endpoint.EventDrivenConsumer       : stopped sayHello\r\n2015-04-08 10:35:32.286  INFO 11513 --- [       Thread-1] o.s.i.endpoint.EventDrivenConsumer       : Removing {service-activator:getHello} as a subscriber to the 'get.hello.channel' channel\r\n2015-04-08 10:35:32.286  INFO 11513 --- [       Thread-1] o.s.integration.channel.DirectChannel    : Channel 'application.get.hello.channel' has 0 subscriber(s).\r\n2015-04-08 10:35:32.286  INFO 11513 --- [       Thread-1] o.s.i.endpoint.EventDrivenConsumer       : stopped getHello\r\n2015-04-08 10:35:32.287  INFO 11513 --- [       Thread-1] o.s.i.endpoint.EventDrivenConsumer       : Removing {logging-channel-adapter:_org.springframework.integration.errorLogger} as a subscriber to the 'errorChannel' channel\r\n2015-04-08 10:35:32.287  INFO 11513 --- [       Thread-1] o.s.i.channel.PublishSubscribeChannel    : Channel 'application.errorChannel' has 0 subscriber(s).\r\n2015-04-08 10:35:32.287  INFO 11513 --- [       Thread-1] o.s.i.endpoint.EventDrivenConsumer       : stopped _org.springframework.integration.errorLogger\r\n2015-04-08 10:35:32.287  INFO 11513 --- [       Thread-1] o.s.c.support.DefaultLifecycleProcessor  : Stopping beans in phase -2147483648\r\n2015-04-08 10:35:32.288  INFO 11513 --- [       Thread-1] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown\r\n2015-04-08 10:35:32.290  INFO 11513 --- [       Thread-1] o.s.s.c.ThreadPoolTaskScheduler          : Shutting down ExecutorService 'taskScheduler'<\/pre>\n<h3>JUnit Example<\/h3>\n<p>We can run the same code as a JUnit test as well. In this case, I&#8217;m allowing Spring to autowire the Spring Integration gateway into my test. Then I have three separate tests to execute our three different hello world examples.<\/p>\n<p>This is more typical of how you would use a Spring Integration gateway in your application code. You can utilize <a title=\"Dependency Injection Example Using Spring\" href=\"http:\/\/springframework.guru\/dependency-injection-example-using-spring\/\">dependency injection<\/a> in Spring to inject an instance of the Spring Integration gateway instance into your class at run time.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\">package guru.springframework.hello.world.si;\r\n\r\nimport guru.springframework.hello.world.si.gateways.HelloWorldGateway;\r\nimport org.junit.Test;\r\nimport org.junit.runner.RunWith;\r\nimport org.springframework.beans.factory.annotation.Autowired;\r\nimport org.springframework.boot.test.SpringApplicationConfiguration;\r\nimport org.springframework.test.context.junit4.SpringJUnit4ClassRunner;\r\n\r\nimport java.util.concurrent.ExecutionException;\r\nimport java.util.concurrent.Future;\r\n\r\n@RunWith(SpringJUnit4ClassRunner.class)\r\n@SpringApplicationConfiguration(classes = HelloWorldSiApplication.class)\r\npublic class HelloWorldSiApplicationTests {\r\n\r\n\t@Autowired\r\n\tHelloWorldGateway helloWorldGateway;\r\n\r\n\t@Test\r\n\tpublic void sayHelloTest() {\r\n\t\thelloWorldGateway.sayHello(\"John\");\r\n\t}\r\n\r\n\t@Test\r\n\tpublic void getHelloMessageTest(){\r\n\t\tString message = helloWorldGateway.getHelloMessage(\"John (get message)\");\r\n\t\tSystem.out.println(message);\r\n\t}\r\n\r\n\t@Test\r\n\tpublic void getHelloAsycTest() throws ExecutionException, InterruptedException {\r\n\t\tFuture helloFuture = helloWorldGateway.getHelloMessageAsync(\"John (Woot, Asyc)\");\r\n\t\tString message = helloFuture.get();\r\n\t\tSystem.out.println(message);\r\n\t}\r\n\r\n}\r\n<\/pre>\n<p>When you run the JUnit tests in this class, you will get the following output in the console.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">  .   ____          _            __ _ _\r\n \/\\\\ \/ ___'_ __ _ _(_)_ __  __ _ \\ \\ \\ \\\r\n( ( )\\___ | '_ | '_| | '_ \\\/ _` | \\ \\ \\ \\\r\n \\\\\/  ___)| |_)| | | | | || (_| |  ) ) ) )\r\n  '  |____| .__|_| |_|_| |_\\__, | \/ \/ \/ \/\r\n =========|_|==============|___\/=\/_\/_\/_\/\r\n :: Spring Boot ::        (v1.2.3.RELEASE)\r\n\r\n2015-04-08 10:44:06.520  INFO 11548 --- [           main] c.i.rt.execution.junit.JUnitStarter      : Starting JUnitStarter on Johns-MacBook-Pro.local with PID 11548 (started by jt in \/Users\/jt\/src\/springframework.guru\/blog\/hello-world-si)\r\n2015-04-08 10:44:06.619  INFO 11548 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@27ae2fd0: startup date [Wed Apr 08 10:44:06 EDT 2015]; root of context hierarchy\r\n2015-04-08 10:44:07.577  INFO 11548 --- [           main] o.s.b.f.xml.XmlBeanDefinitionReader      : Loading XML bean definitions from URL [file:\/Users\/jt\/src\/springframework.guru\/blog\/hello-world-si\/target\/classes\/spring\/si-config.xml]\r\n2015-04-08 10:44:07.875  INFO 11548 --- [           main] o.s.b.f.config.PropertiesFactoryBean     : Loading properties file from URL [jar:file:\/Users\/jt\/.m2\/repository\/org\/springframework\/integration\/spring-integration-core\/4.1.2.RELEASE\/spring-integration-core-4.1.2.RELEASE.jar!\/META-INF\/spring.integration.default.properties]\r\n2015-04-08 10:44:07.885  INFO 11548 --- [           main] o.s.i.config.IntegrationRegistrar        : No bean named 'integrationHeaderChannelRegistry' has been explicitly defined. Therefore, a default DefaultHeaderChannelRegistry will be created.\r\n2015-04-08 10:44:08.123  INFO 11548 --- [           main] faultConfiguringBeanFactoryPostProcessor : No bean named 'errorChannel' has been explicitly defined. Therefore, a default PublishSubscribeChannel will be created.\r\n2015-04-08 10:44:08.126  INFO 11548 --- [           main] faultConfiguringBeanFactoryPostProcessor : No bean named 'taskScheduler' has been explicitly defined. Therefore, a default ThreadPoolTaskScheduler will be created.\r\n2015-04-08 10:44:08.289  INFO 11548 --- [           main] o.s.b.f.config.PropertiesFactoryBean     : Loading properties file from URL [jar:file:\/Users\/jt\/.m2\/repository\/org\/springframework\/integration\/spring-integration-core\/4.1.2.RELEASE\/spring-integration-core-4.1.2.RELEASE.jar!\/META-INF\/spring.integration.default.properties]\r\n2015-04-08 10:44:08.290  INFO 11548 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'integrationGlobalProperties' of type [class org.springframework.beans.factory.config.PropertiesFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)\r\n2015-04-08 10:44:08.291  INFO 11548 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'integrationGlobalProperties' of type [class java.util.Properties] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)\r\n2015-04-08 10:44:08.295  INFO 11548 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'messageBuilderFactory' of type [class org.springframework.integration.support.DefaultMessageBuilderFactory] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)\r\n2015-04-08 10:44:08.537  INFO 11548 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean '(inner bean)#76012793' of type [class org.springframework.integration.channel.MessagePublishingErrorHandler] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)\r\n2015-04-08 10:44:08.538  INFO 11548 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService  'taskScheduler'\r\n2015-04-08 10:44:08.541  INFO 11548 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'taskScheduler' of type [class org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)\r\n2015-04-08 10:44:08.542  INFO 11548 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'integrationHeaderChannelRegistry' of type [class org.springframework.integration.channel.DefaultHeaderChannelRegistry] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)\r\n2015-04-08 10:44:08.991  INFO 11548 --- [           main] ProxyFactoryBean$MethodInvocationGateway : started helloWorldGateway\r\n2015-04-08 10:44:08.991  INFO 11548 --- [           main] ProxyFactoryBean$MethodInvocationGateway : started helloWorldGateway\r\n2015-04-08 10:44:08.991  INFO 11548 --- [           main] ProxyFactoryBean$MethodInvocationGateway : started helloWorldGateway\r\n2015-04-08 10:44:08.991  INFO 11548 --- [           main] o.s.i.gateway.GatewayProxyFactoryBean    : started helloWorldGateway\r\n2015-04-08 10:44:09.309  INFO 11548 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase -2147483648\r\n2015-04-08 10:44:09.312  INFO 11548 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 0\r\n2015-04-08 10:44:09.312  INFO 11548 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : Adding {service-activator:sayHello} as a subscriber to the 'say.hello.channel' channel\r\n2015-04-08 10:44:09.313  INFO 11548 --- [           main] o.s.integration.channel.DirectChannel    : Channel 'application:-1.say.hello.channel' has 1 subscriber(s).\r\n2015-04-08 10:44:09.314  INFO 11548 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : started sayHello\r\n2015-04-08 10:44:09.314  INFO 11548 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : Adding {service-activator:getHello} as a subscriber to the 'get.hello.channel' channel\r\n2015-04-08 10:44:09.314  INFO 11548 --- [           main] o.s.integration.channel.DirectChannel    : Channel 'application:-1.get.hello.channel' has 1 subscriber(s).\r\n2015-04-08 10:44:09.315  INFO 11548 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : started getHello\r\n2015-04-08 10:44:09.315  INFO 11548 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : Adding {logging-channel-adapter:_org.springframework.integration.errorLogger} as a subscriber to the 'errorChannel' channel\r\n2015-04-08 10:44:09.316  INFO 11548 --- [           main] o.s.i.channel.PublishSubscribeChannel    : Channel 'application:-1.errorChannel' has 1 subscriber(s).\r\n2015-04-08 10:44:09.316  INFO 11548 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : started _org.springframework.integration.errorLogger\r\n2015-04-08 10:44:09.326  INFO 11548 --- [           main] c.i.rt.execution.junit.JUnitStarter      : Started JUnitStarter in 3.267 seconds (JVM running for 4.367)\r\nHello John (Woot, Asyc)!!\r\nHello John!!\r\nHello John (get message)!!\r\n2015-04-08 10:44:09.367  INFO 11548 --- [       Thread-1] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@27ae2fd0: startup date [Wed Apr 08 10:44:06 EDT 2015]; root of context hierarchy\r\n2015-04-08 10:44:09.368  INFO 11548 --- [       Thread-1] o.s.c.support.DefaultLifecycleProcessor  : Stopping beans in phase 0\r\n2015-04-08 10:44:09.369  INFO 11548 --- [       Thread-1] ProxyFactoryBean$MethodInvocationGateway : stopped helloWorldGateway\r\n2015-04-08 10:44:09.369  INFO 11548 --- [       Thread-1] ProxyFactoryBean$MethodInvocationGateway : stopped helloWorldGateway\r\n2015-04-08 10:44:09.369  INFO 11548 --- [       Thread-1] ProxyFactoryBean$MethodInvocationGateway : stopped helloWorldGateway\r\n2015-04-08 10:44:09.369  INFO 11548 --- [       Thread-1] o.s.i.gateway.GatewayProxyFactoryBean    : stopped helloWorldGateway\r\n2015-04-08 10:44:09.369  INFO 11548 --- [       Thread-1] o.s.i.endpoint.EventDrivenConsumer       : Removing {service-activator:sayHello} as a subscriber to the 'say.hello.channel' channel\r\n2015-04-08 10:44:09.369  INFO 11548 --- [       Thread-1] o.s.integration.channel.DirectChannel    : Channel 'application:-1.say.hello.channel' has 0 subscriber(s).\r\n2015-04-08 10:44:09.369  INFO 11548 --- [       Thread-1] o.s.i.endpoint.EventDrivenConsumer       : stopped sayHello\r\n2015-04-08 10:44:09.369  INFO 11548 --- [       Thread-1] o.s.i.endpoint.EventDrivenConsumer       : Removing {service-activator:getHello} as a subscriber to the 'get.hello.channel' channel\r\n2015-04-08 10:44:09.369  INFO 11548 --- [       Thread-1] o.s.integration.channel.DirectChannel    : Channel 'application:-1.get.hello.channel' has 0 subscriber(s).\r\n2015-04-08 10:44:09.370  INFO 11548 --- [       Thread-1] o.s.i.endpoint.EventDrivenConsumer       : stopped getHello\r\n2015-04-08 10:44:09.370  INFO 11548 --- [       Thread-1] o.s.i.endpoint.EventDrivenConsumer       : Removing {logging-channel-adapter:_org.springframework.integration.errorLogger} as a subscriber to the 'errorChannel' channel\r\n2015-04-08 10:44:09.370  INFO 11548 --- [       Thread-1] o.s.i.channel.PublishSubscribeChannel    : Channel 'application:-1.errorChannel' has 0 subscriber(s).\r\n2015-04-08 10:44:09.370  INFO 11548 --- [       Thread-1] o.s.i.endpoint.EventDrivenConsumer       : stopped _org.springframework.integration.errorLogger\r\n2015-04-08 10:44:09.370  INFO 11548 --- [       Thread-1] o.s.c.support.DefaultLifecycleProcessor  : Stopping beans in phase -2147483648\r\n2015-04-08 10:44:09.373  INFO 11548 --- [       Thread-1] o.s.s.c.ThreadPoolTaskScheduler          : Shutting down ExecutorService 'taskScheduler'<\/pre>\n<h2>Conclusion<\/h2>\n<p>This has been a very simple Hello World example using Spring Integration. While this post only scratches the surface of the abilities of Spring Integration, I hope you can see how easy it is to utilize Spring Integration in your code. As you learn to develop large scale enterprise applications, you will find that Spring Integration is an important module in the Spring Framework.<\/p>\n<h2>Get The\u00a0Code<\/h2>\n<p>I&#8217;ve committed the source code for this post to GitHub. It is a Maven project which you can download and build. If you wish to learn more about the Spring Framework, I am a free introduction to Spring tutorial. You can sign up for this tutorial in the section below.<\/p>\n\r\n\t<div class=\"df_call_to_action df_content_element cta_square\"  data-cta-class=\"normal\" data-cta-bg-color=\"#0F0F0F\" data-cta-border-color=\"#0F0F0F\">\r\n\t    <div class=\"cta_wrapper\">\r\n\t    \t<div class=\"cta_header\"><h3 style=\"color:#6cb44a !important;\">Source Code<\/h3><\/div>\r\n\t\t    <div class=\"cta_content\">The source code for this post is available on github. You can download it <a title=\"Defining Spring Beans\" href=\"https:\/\/github.com\/springframeworkguru\/hello-world-si\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>.<\/div>\r\n\t    <\/div>\r\n\t     \r\n\t<\/div>\r\n\n<h3><\/h3>\n","protected":false},"excerpt":{"rendered":"<p>Spring Integration is a very powerful module in the Spring Framework. It was originally inspired by pivotal book in computer science called Enterprise Integration Patterns written by Gregor Hohpe and Bobby Woolf. While the Gang of Four focused their work on software design patterns, Enterprise Integration Patterns is focused on how disparate systems communicate with [&hellip;]<a href=\"https:\/\/springframework.guru\/hello-world-using-spring-integration\/\" class=\"df-link-excerpt\">Continue reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":4586,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"Hello World Using Spring Integration http:\/\/wp.me\/p5BZrZ-d9 #springintegration #spring #springframework #springio #java","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[21,58],"tags":[42,27,60,40,29,30,59],"class_list":["post-815","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-spring","category-spring-integration","tag-hello-world","tag-java","tag-junit","tag-spring","tag-spring-boot","tag-spring-framework","tag-spring-integration"],"jetpack_publicize_connections":[],"aioseo_notices":[],"modified_by":"Simanta","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/springframework.guru\/wp-content\/uploads\/2015\/03\/Banner560x292_03web.jpg","jetpack_shortlink":"https:\/\/wp.me\/p5BZrZ-d9","_links":{"self":[{"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/posts\/815"}],"collection":[{"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/comments?post=815"}],"version-history":[{"count":10,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/posts\/815\/revisions"}],"predecessor-version":[{"id":5772,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/posts\/815\/revisions\/5772"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/media\/4586"}],"wp:attachment":[{"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/media?parent=815"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/categories?post=815"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/tags?post=815"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}