spring 集成超时客户端 [英] spring integration time out clients
问题描述
我使用 spring 集成的场景是:
My scenario with spring integration is:
- 使用自定义协议(大小和内容)发送数据的数十个生产者
- 我必须解码这个自定义协议,然后处理结果.
所以我尝试了很多配置,目前最好的配置如下:
So I tried lots configuration, the best for this moment is the follow:
<bean id="serializer" class="com.MySerializerDeserializer" />
<task:executor id="myTaskExecutor" pool-size="5-300" queue-capacity="500000"/>
<int-ip:tcp-connection-factory id="serverTcpConFact"
type="server"
port="5566"
using-nio="true"
single-use="false"
so-timeout="5000"
task-executor="myTaskExecutor"
deserializer="serializer"
serializer="serializer"/>
<int-ip:tcp-inbound-channel-adapter id="tcpInboundAdapter"
channel="tcpInbound"
connection-factory="serverTcpConFact" />
<int:channel id="tcpInbound" />
<int:service-activator input-channel="tcpInbound"
ref="importService"
method="handler" />
<bean id="importService" class="com.MyImportService" />
序列化类为:
public class MySerializerDeserializer implements Serializer< MyMessage >, Deserializer< MyMessage > {
@Override
public MyMessage deserialize(InputStream inputStream) throws IOException {
DataInputStream dis = new DataInputStream(inputStream);
int size = dis.readInt();
byte[] b = new byte[size];
dis.read(b);
MyMessage s = new MyMessage ();
String value = new String(b);
s.setTest(value);
return s;
我使用此代码连接服务器:
I use this code to thest the server:
class SimpleThread extends Thread {
public void run() {
try {
String id = java.util.UUID.randomUUID().toString();
try (Socket echoSocket = new Socket("localhost", 5566)) {
DataOutputStream dos = new DataOutputStream(echoSocket.getOutputStream());
for (int i = 0; i < 500; i++) {
String s = id + " " + i;
dos.writeInt(s.length());
dos.write(s.getBytes());
dos.flush();
System.out.println(id + " - " + i + " - " + s.length());
}
}
当我尝试执行多个线程时,当我使用单个线程执行此操作时效果很好,例如:
When I execute this with a single thread works fine when I try to execute more than one threads Like:
for (int i = 0; i < 5; i++) {
new SimpleThread().start();
}
spring 集成服务器卡住了,我有以下警告:
The spring integration server gets stuck and I have the follow WARN:
WARN _[m [THREAD ID=myTaskExecutor-1] 2014-06-25 12:42:18 WARN org.springframework.integration.ip.tcp.connection.AbstractConnectionFactory:566 - Timing out TcpNioConnection 127.0.0.1:56273:5566:4e3caf61-1101-4881-a1cf-1c31610b33f3
它不起作用,服务器无法接收消息.
And it doesn’t work, The server can't recive the messages.
出现这个错误:
我哪里错了?谢谢.
编辑
我这样修改了线程轮询:
I modified the thread poll in this way:
<bean id="myTaskExecutor" class="org.springframework.integration.util.CompositeExecutor">
<constructor-arg>
<bean class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="threadNamePrefix" value="io-" />
<property name="corePoolSize" value="4" />
<property name="maxPoolSize" value="8" />
<property name="queueCapacity" value="50000" />
<property name="rejectedExecutionHandler">
<bean class="org.springframework.integration.util.CallerBlocksPolicy">
<constructor-arg value="10000" />
</bean>
</property>
</bean>
</constructor-arg>
<constructor-arg>
<bean class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="threadNamePrefix" value="assembler-" />
<property name="corePoolSize" value="4" />
<property name="maxPoolSize" value="10" />
<property name="queueCapacity" value="50000" />
<property name="rejectedExecutionHandler">
<bean class="org.springframework.integration.util.CallerBlocksPolicy">
<constructor-arg value="10000" />
</bean>
</property>
</bean>
</constructor-arg>
</bean>
而且服务器反应更快,但我仍然有问题
And the server is more responvive, but the I still have the problem
**编辑*****
进口服务为:
public class ImportService {
public void handler(MyMessage inp) {
System.out.println(Thread.currentThread().getName() + "******" + inp.getTest());
}
推荐答案
我刚刚完全按照描述运行了您的测试(复制了您的代码),并且一切都按预期工作.500 条消息发送到另一端.
I just ran your test exactly as described (copied your code) and all worked as expected. 500 messages delivered to the other side.
我建议您打开 TRACE 级别的日志记录,并可能在您的解串器中添加一些调试日志记录.
I suggest you turn on TRACE level logging, and maybe add some debug logging to your deserializer.
这篇关于spring 集成超时客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!