gpt4 book ai didi

java - 如何在Java类配置中定义 `TcpOutboundGateway` bean

转载 作者:行者123 更新时间:2023-11-30 08:09:28 25 4
gpt4 key购买 nike

当我尝试定义 TcpOutboundGateway bean 时,我发现该类没有 requestChannelreplyChannelName setter 。如何在 Java 类配置中正确定义该 bean?

哪个 Java 类配置与下面提供的 XML 配置相同?

<int-ip:tcp-outbound-gateway id="outGateway"
request-channel="input"
reply-channel="clientBytes2StringChannel"
connection-factory="client"
request-timeout="10000"
reply-timeout="10000"/>

最佳答案

此代码等于 link 提供的客户端 xml 配置。 .

public static final String STRING_TO_BYTES_CHANNEL = "stringToBytesChannel";
public static final String REQUEST_CHANNEL = "requestChannel";

private String host = "localhost";
private int port = 2020;

@Bean
public TcpNetClientConnectionFactory connectionFactory() {
TcpNetClientConnectionFactory factory = new TcpNetClientConnectionFactory(host, port);
factory.setSingleUse(true);
factory.setSoTimeout(10000);
return factory;
}

@Bean
public MessageChannel requestChannel() {
return new DirectChannel();
}

@Bean
public MessageChannel stringToBytesChannel() {
return new DirectChannel();
}

@Bean
@Transformer(inputChannel = STRING_TO_BYTES_CHANNEL)
public ObjectToStringTransformer objectToStringTransformer() {
return new ObjectToStringTransformer();
}

@Bean
@ServiceActivator(inputChannel = REQUEST_CHANNEL)
public TcpOutboundGateway outboundGateway() {
TcpOutboundGateway gateway = new TcpOutboundGateway();
gateway.setConnectionFactory(connectionFactory());
gateway.setReplyChannel(stringToBytesChannel());
gateway.setRequestTimeout(10000);
gateway.setRemoteTimeout(10000);
return gateway;
}

@MessagingGateway(defaultRequestChannel = REQUEST_CHANNEL)
public interface RequestGateway {
String send(String message);
}

关于java - 如何在Java类配置中定义 `TcpOutboundGateway` bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30604418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com