我正在使用
给出此代码以动态生成TCP连接。我定义了一个richricherHeader,它具有一个称为“ connectionId”的唯一标头。此“ connectionId”将是创建的连接的名称。
@Service
@EnableIntegration
public class TcpConnectionsHolder {
private static final Logger LOGGER = LoggerFactory.getLogger(TcpConnectionsHolder.class);
@Autowired
private IntegrationFlowContext flowContext;
// @Autowired
// private BridgeMessageEndpoint bme;
@Value("${socket.tcp.headBytes}")
private int headBytes;
@Value("${socket.tcp.retryInterval}")
private int retryInterval;
//@Qualifier("sendToApi")
@Autowired
private MessageChannel fromTcp;
private final static String CONNECTION_SUFFIX="_connection";
public IntegrationFlow createTcpConnection(String connectionId, String host, int port, int headBytes,
int retryInterval)
{
LOGGER.debug("createTcpConnection -> connectionId: {} - host: {} - port: {} - headBytes: {} - retryInterval: {}"
,connectionId,host,port,headBytes,retryInterval);
IntegrationFlow ifr = existsConnection(connectionId);
if (ifr == null) {
TcpNetClientConnectionFactory cf = new TcpNetClientConnectionFactory(host, port);
final ByteArrayLengthHeaderSerializer by = new ByteArrayLengthHeaderSerializer(headBytes);
cf.setSingleUse(false);
cf.setSoKeepAlive(true);
cf.setSerializer(by);
cf.setDeserializer(by);
cf.setComponentName(connectionId);
//Inbound Adapter
TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
adapter.setConnectionFactory(cf);
adapter.setClientMode(true);
adapter.setErrorChannelName("errorChannel");
adapter.setRetryInterval(retryInterval);
adapter.setOutputChannel(fromTcp);
//OutBound Adapter
TcpSendingMessageHandler sender = new TcpSendingMessageHandler();
sender.setConnectionFactory(cf);
ifr = IntegrationFlows
.from(adapter)
.enrichHeaders(h -> h.header("connec_id",connectionId))
.handle(sender).get();
this.flowContext.registration(ifr).id(connectionId+CONNECTION_SUFFIX).addBean(cf).register();
LOGGER.debug("createTcpConnection: Connection created");
}
return ifr;
}
我还有一个MessageEndpoint类,其中定义了所有serviceActivator方法
@Configuration
@MessageEndpoint
@EnableIntegration
public class BridgeMessageEndpoint{
private static final Logger LOGGER = LoggerFactory.getLogger(BridgeMessageEndpoint.class);
@Autowired
private ApplicationContext applicationContext;
@ServiceActivator(inputChannel = "fromTcp")
public void outbound(final String inMessage, final @Headers Map<String, Object> headerMap) {
sendToApi(inMessage, headerMap);
}
private void sendToApi(final String inMessage, final Map<String, Object> headerMap) {
LOGGER.debug("Recuperando el mensaje Hex {}", inMessage);
final PaymentOrder paymentOrder = new PaymentOrder();
paymentOrder.setMessage(inMessage);
final SplitterRestClient splitterRestClient = applicationContext.getBean(SplitterRestClient.class);
splitterRestClient.reportPayment(paymentOrder, headerMap);
}
@Bean
@ServiceActivator(inputChannel = "errorChannel")
public MessageHandler logger() {
final LoggingHandler loggingHandler = new LoggingHandler(LoggingHandler.Level.DEBUG.name());
loggingHandler.setLoggerName("Log");
return loggingHandler;
}
/*
* Calls TcpRouter to redirect the message to the correct TCP connection
*/
@Bean
public IntegrationFlow toTcp() {
return f -> f.route(new TcpRouter());
}
@Bean
public MessageChannel fromTcp() {
final DirectChannel channel = new DirectChannel();
channel.addInterceptor(new ChannelInterceptorAdapter() {
@Override
public Message<?> preSend(final Message<?> message, final MessageChannel channel) {
// Parse Message byte[] to StringHex
final byte[] bMessagePayload = (byte[]) message.getPayload();
return MessageBuilder.withPayload(Hex.encodeHexString(bMessagePayload))
.copyHeaders(message.getHeaders()).build();
}
});
return channel;
}
话虽如此。我有几个问题需要您的帮助。
在creationTcpconnection方法中,我试图创建动态连接,定义接收方适配器和SenderAdapter。
如果按照我的建议注释掉该行(adapter.setOutputChannel(fromTcp);),则对BridgeMessageEndpoint类中定义的serviceActivator(inputChannel =“ fromTcp”)的执行不起作用。
正如我之前评论过的那样,我需要发送一个新的标头,称为“ connectionId”,该标头标识哪个连接已从tcp服务器接收到消息,以便稍后响应该相同的连接