|
| 1 | +package org.spring.integration.dynamic.tcp; |
| 2 | + |
| 3 | +import org.springframework.core.serializer.Serializer; |
| 4 | +import org.springframework.expression.EvaluationContext; |
| 5 | +import org.springframework.expression.Expression; |
| 6 | +import org.springframework.expression.spel.support.StandardEvaluationContext; |
| 7 | +import org.springframework.integration.dsl.IntegrationFlow; |
| 8 | +import org.springframework.integration.dsl.context.IntegrationFlowContext; |
| 9 | +import org.springframework.integration.expression.ValueExpression; |
| 10 | +import org.springframework.integration.handler.MessageProcessor; |
| 11 | +import org.springframework.integration.ip.dsl.Tcp; |
| 12 | +import org.springframework.integration.ip.dsl.TcpClientConnectionFactorySpec; |
| 13 | +import org.springframework.integration.ip.dsl.TcpOutboundGatewaySpec; |
| 14 | +import org.springframework.integration.ip.tcp.serializer.AbstractByteArraySerializer; |
| 15 | +import org.springframework.integration.ip.tcp.serializer.ByteArrayCrLfSerializer; |
| 16 | +import org.springframework.messaging.Message; |
| 17 | +import org.springframework.messaging.MessageChannel; |
| 18 | +import org.springframework.util.Assert; |
| 19 | + |
| 20 | +import java.util.Map; |
| 21 | +import java.util.concurrent.ConcurrentHashMap; |
| 22 | +import java.util.concurrent.Executors; |
| 23 | + |
| 24 | +public class DynamicTcpOutboundGateway implements MessageProcessor<MessageChannel> { |
| 25 | + public static final String DEFAULT_RESPONSE_CHANNEL_NAME = "serverResponseChannel"; |
| 26 | + private final Map<String, IntegrationFlowContext.IntegrationFlowRegistration> flowCache = new ConcurrentHashMap<>(); |
| 27 | + private final IntegrationFlowContext flowContext; |
| 28 | + private final EvaluationContext evaluationContext = new StandardEvaluationContext(); |
| 29 | + private String responseChannel = DEFAULT_RESPONSE_CHANNEL_NAME; |
| 30 | + private AbstractByteArraySerializer deserializer = new ByteArrayCrLfSerializer(); |
| 31 | + private Serializer<?> serializer; |
| 32 | + private Expression hostExpression; |
| 33 | + private Expression portExpression; |
| 34 | + private Expression cacheableExpression = new ValueExpression<>(Boolean.FALSE); |
| 35 | + |
| 36 | + public DynamicTcpOutboundGateway(IntegrationFlowContext flowContext) { |
| 37 | + this.flowContext = flowContext; |
| 38 | + |
| 39 | + } |
| 40 | + |
| 41 | + private static String getFlowId(String host, Integer port) { |
| 42 | + return String.format("flow_%s_%d.chl", host, port); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public MessageChannel processMessage(Message<?> message) { |
| 47 | + final String host = getHost(message); |
| 48 | + final int port = getPort(message); |
| 49 | + final boolean cacheable = isCacheable(message); |
| 50 | + final String flowId = getFlowId(host, port) + (cacheable ? "" : System.nanoTime()); |
| 51 | + |
| 52 | + final TcpClientConnectionFactorySpec connectionFactorySpec = Tcp.netClient(host, port); |
| 53 | + final TcpOutboundGatewaySpec gatewaySpec = getMessageHandlerSpec(connectionFactorySpec); |
| 54 | + IntegrationFlowContext.IntegrationFlowRegistration registration; |
| 55 | + if (!cacheable) { |
| 56 | + registration = createNonCacheableConnectionFlow(flowId, gatewaySpec); |
| 57 | + } else { |
| 58 | + registration = flowCache.computeIfAbsent(flowId, id -> createConnectionFlow(id, gatewaySpec)); |
| 59 | + |
| 60 | + } |
| 61 | + return registration.getInputChannel(); |
| 62 | + } |
| 63 | + |
| 64 | + private IntegrationFlowContext.IntegrationFlowRegistration createConnectionFlow(String flowId, TcpOutboundGatewaySpec gatewaySpec) { |
| 65 | + final IntegrationFlow flow = f -> f |
| 66 | + .handle(gatewaySpec) |
| 67 | + .channel(responseChannel); |
| 68 | + return flowContext |
| 69 | + .registration(flow) |
| 70 | + .id(flowId) |
| 71 | + .register(); |
| 72 | + } |
| 73 | + |
| 74 | + private IntegrationFlowContext.IntegrationFlowRegistration createNonCacheableConnectionFlow(String flowId, TcpOutboundGatewaySpec gatewaySpec) { |
| 75 | + final IntegrationFlow flow = f -> f |
| 76 | + .handle(gatewaySpec) |
| 77 | + .publishSubscribeChannel(Executors.newCachedThreadPool(), s -> s.subscribe(subFlow -> subFlow.channel(responseChannel))) |
| 78 | + .handle(message -> flowContext.remove(flowId)); |
| 79 | + return flowContext |
| 80 | + .registration(flow) |
| 81 | + .id(flowId) |
| 82 | + .register(); |
| 83 | + } |
| 84 | + |
| 85 | + private TcpOutboundGatewaySpec getMessageHandlerSpec(TcpClientConnectionFactorySpec connectionFactorySpec) { |
| 86 | + final TcpClientConnectionFactorySpec connectionFactory = connectionFactorySpec |
| 87 | + .singleUseConnections(true); |
| 88 | + if (serializer != null) { |
| 89 | + connectionFactory.serializer(serializer); |
| 90 | + } |
| 91 | + if (deserializer != null) { |
| 92 | + connectionFactory.deserializer(deserializer); |
| 93 | + } |
| 94 | + return Tcp.outboundGateway(connectionFactory); |
| 95 | + } |
| 96 | + |
| 97 | + private boolean isCacheable(Message<?> requestMessage) { |
| 98 | + if (this.cacheableExpression != null) { |
| 99 | + final Boolean value = this.cacheableExpression.getValue(this.evaluationContext, requestMessage, Boolean.class); |
| 100 | + if (value != null) { |
| 101 | + return value; |
| 102 | + } |
| 103 | + } |
| 104 | + return true; |
| 105 | + } |
| 106 | + |
| 107 | + private String getHost(Message<?> requestMessage) { |
| 108 | + Assert.state(this.hostExpression != null, "host expression is missing"); |
| 109 | + final String host = this.hostExpression.getValue(this.evaluationContext, requestMessage, String.class); |
| 110 | + Assert.state(host != null, "host is missing"); |
| 111 | + return host; |
| 112 | + } |
| 113 | + |
| 114 | + private int getPort(Message<?> requestMessage) { |
| 115 | + Assert.state(this.portExpression != null, "port expression is missing"); |
| 116 | + final Integer port = this.portExpression.getValue(this.evaluationContext, requestMessage, Integer.class); |
| 117 | + Assert.state(port != null, "port is missing"); |
| 118 | + return port; |
| 119 | + } |
| 120 | + |
| 121 | + public void setDeserializer(AbstractByteArraySerializer deserializer) { |
| 122 | + this.deserializer = deserializer; |
| 123 | + } |
| 124 | + |
| 125 | + public void setResponseChannel(String responseChannel) { |
| 126 | + this.responseChannel = responseChannel; |
| 127 | + } |
| 128 | + |
| 129 | + public void setHostExpression(Expression hostExpression) { |
| 130 | + this.hostExpression = hostExpression; |
| 131 | + } |
| 132 | + |
| 133 | + public void setPortExpression(Expression portExpression) { |
| 134 | + this.portExpression = portExpression; |
| 135 | + } |
| 136 | + |
| 137 | + public void setSerializer(Serializer<?> serializer) { |
| 138 | + this.serializer = serializer; |
| 139 | + } |
| 140 | + |
| 141 | + public void setCacheableExpression(Expression cacheableExpression) { |
| 142 | + this.cacheableExpression = cacheableExpression; |
| 143 | + } |
| 144 | + |
| 145 | +} |
0 commit comments