|
| 1 | +/* |
| 2 | + * Copyright 2025-present the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.integration.samples.controlbus.config; |
| 17 | + |
| 18 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 19 | +import org.springframework.context.annotation.Bean; |
| 20 | +import org.springframework.context.annotation.Configuration; |
| 21 | +import org.springframework.context.annotation.Profile; |
| 22 | +import org.springframework.integration.channel.DirectChannel; |
| 23 | +import org.springframework.integration.channel.QueueChannel; |
| 24 | +import org.springframework.integration.config.EnableIntegration; |
| 25 | +import org.springframework.integration.dsl.IntegrationFlow; |
| 26 | +import org.springframework.integration.dsl.Pollers; |
| 27 | +import org.springframework.messaging.MessageChannel; |
| 28 | + |
| 29 | +/** |
| 30 | + * Java-based Spring Integration configuration for the Control Bus sample. |
| 31 | + * <p> |
| 32 | + * This configuration demonstrates the Control Bus component functionality, |
| 33 | + * which uses SpEL expressions to control Spring Integration endpoints |
| 34 | + * (e.g., start/stop an inbound adapter). |
| 35 | + * <p> |
| 36 | + * Activate this configuration using the "java-config" profile. |
| 37 | + * <p> |
| 38 | + * This configuration is functionally equivalent to {@code ControlBusDemo-context.xml} |
| 39 | + * and defines: |
| 40 | + * <ul> |
| 41 | + * <li>A control channel for sending control messages</li> |
| 42 | + * <li>An output channel (queue-based) for the inbound adapter</li> |
| 43 | + * <li>A control bus endpoint that processes control messages</li> |
| 44 | + * <li>An inbound channel adapter that generates "Hello" messages at a fixed rate</li> |
| 45 | + * </ul> |
| 46 | + * |
| 47 | + * @author Glenn Renfro |
| 48 | + */ |
| 49 | +@Configuration |
| 50 | +@EnableIntegration |
| 51 | +@Profile("java-config") |
| 52 | +public class ControlBusConfiguration { |
| 53 | + |
| 54 | + /** |
| 55 | + * Create a direct channel for sending control messages to the control bus. |
| 56 | + * <p> |
| 57 | + * This channel is used to send SpEL expressions that control Spring Integration |
| 58 | + * endpoints (e.g., {@code @inboundAdapter.start()} or {@code @inboundAdapter.stop()}). |
| 59 | + * |
| 60 | + * @return a DirectChannel for control messages |
| 61 | + */ |
| 62 | + @Bean |
| 63 | + public MessageChannel controlChannel() { |
| 64 | + return new DirectChannel(); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Create a queue-based channel for receiving messages from the inbound adapter. |
| 69 | + * <p> |
| 70 | + * This channel uses a queue to allow asynchronous message consumption. |
| 71 | + * |
| 72 | + * @return a QueueChannel for adapter output |
| 73 | + */ |
| 74 | + @Bean |
| 75 | + public MessageChannel adapterOutputChannel() { |
| 76 | + return new QueueChannel(); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Create an integration flow that connects the control channel to the control bus. |
| 81 | + * <p> |
| 82 | + * Messages sent to the control channel are processed by the control bus, |
| 83 | + * which evaluates SpEL expressions to control Spring Integration endpoints. |
| 84 | + * |
| 85 | + * @param controlChannel input channel for control commands |
| 86 | + * @return an IntegrationFlow connecting controlChannel to controlBus |
| 87 | + */ |
| 88 | + @Bean |
| 89 | + public IntegrationFlow controlBusFlow(@Qualifier("controlChannel") MessageChannel controlChannel) { |
| 90 | + return IntegrationFlow.from(controlChannel) |
| 91 | + .controlBus() |
| 92 | + .get(); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Create an inbound channel adapter that generates messages. |
| 97 | + * <p> |
| 98 | + * The adapter: |
| 99 | + * <ul> |
| 100 | + * <li>Generates the string "Hello" as the payload</li> |
| 101 | + * <li>Sends messages to the adapterOutputChannel</li> |
| 102 | + * <li>Starts with auto-startup disabled (must be started via control bus)</li> |
| 103 | + * <li>Polls at a fixed rate of 1000ms</li> |
| 104 | + * </ul> |
| 105 | + * |
| 106 | + * @return an IntegrationFlow representing the inbound adapter |
| 107 | + */ |
| 108 | + @Bean |
| 109 | + public IntegrationFlow inboundAdapter() { |
| 110 | + return IntegrationFlow.fromSupplier(() -> "Hello", |
| 111 | + c -> c.id("inboundAdapter") |
| 112 | + .autoStartup(false) |
| 113 | + .poller(Pollers.fixedRate(1000))) |
| 114 | + .channel(adapterOutputChannel()) |
| 115 | + .get(); |
| 116 | + } |
| 117 | + |
| 118 | +} |
| 119 | + |
0 commit comments