gpt4 book ai didi

java - Spring TCP 支持 CPU 利用率高

转载 作者:行者123 更新时间:2023-12-02 11:29:12 24 4
gpt4 key购买 nike

我正在使用 spring TCP support创建 TCP 服务器。

我注意到,当我只发送一个请求时,CPU 的运行率为 91%。这是我的代码

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.IntegrationComponentScan;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.ip.tcp.TcpInboundGateway;
import org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory;
import org.springframework.integration.ip.tcp.connection.MessageConvertingTcpMessageMapper;
import org.springframework.integration.ip.tcp.connection.TcpNetServerConnectionFactory;
import org.springframework.integration.ip.tcp.serializer.AbstractByteArraySerializer;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.converter.ByteArrayMessageConverter;
import org.springframework.messaging.converter.MessageConverter;

@Configuration
@IntegrationComponentScan
public class TCPServerConfig {

@Value("${tcp.listener.port}")
private int port;

@Bean
public MessageConvertingTcpMessageMapper mapper(final MessageConverter messageConverter) {
return new MessageConvertingTcpMessageMapper(messageConverter);
}

@Bean
public MessageConverter messageConverter() {
return new ByteArrayMessageConverter();
}


@Bean
public TcpInboundGateway tcpInGate(final AbstractServerConnectionFactory connectionFactory) {
final TcpInboundGateway inGate = new TcpInboundGateway();
inGate.setConnectionFactory(connectionFactory);
inGate.setRequestChannel(fromTcp());

return inGate;
}

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

@Bean
public AbstractServerConnectionFactory serverCF(
final AbstractByteArraySerializer byteArraySerializer) {
final TcpNetServerConnectionFactory connectionFactory =
new TcpNetServerConnectionFactory(this.port);
connectionFactory.setDeserializer(byteArraySerializer);
connectionFactory.setSerializer(byteArraySerializer);
return connectionFactory;
}

@Bean
public AbstractByteArraySerializer byteArraySerializer() {
return new ByteArrayCustomeSerializer();
}
}

这就是我转换和记录消息的方式

import static org.apache.commons.lang3.StringUtils.EMPTY;
import static org.apache.commons.lang3.StringUtils.isBlank;


import java.nio.charset.Charset;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;

import org.springframework.integration.annotation.MessageEndpoint;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.annotation.Transformer;

@Slf4j
@MessageEndpoint
class TCPMessageProcessor {

@Transformer(inputChannel = "fromTcp", outputChannel = "toProcess")
public String convertInput(final byte[] bytes) {
if(ArrayUtils.isEmpty(bytes)){
return EMPTY;
}

String inboundMessage = new String(bytes, Charset.forName("ASCII"));
log.info("Converted the message to string: '{}'. Handing it processor", inboundMessage);
return inboundMessage;
}

@ServiceActivator(inputChannel = "toProcess")
public String process(final String message) {
if(isBlank(message)){
return EMPTY;
}

log.info("Started processing message '{}'", message);

return "some response";
}
}

我添加日志记录,发现应用程序收到第一个请求后,它会正确提供该请求,然后每秒使用空字节数组的输入流多次调用ByteArrayCustomeSerializer#doDeserialize 。任何人都可以提供有关为什么会发生这种情况以及如何避免这种行为的见解吗?

这来自org.springframework.integration.ip.tcp.connection.TcpNetConnection

  "threadId":"pool-1-thread-3", "message":"Message received GenericMessage [payload=byte[0], headers={ip_tcp_remotePort=*****, ip_connectionId=localhost:*****:*****:d313c398-fc80-48dd-b9c1-f447c9172f09, ip_localInetAddress=/127.0.0.1, ip_address=127.0.0.1, id=1fd69791-c300-787b-c5cc-281a360ae8f4, ip_hostname=localhost, timestamp=1521623822108}]"

最佳答案

这很可能是您的 ByteArrayCustomeSerializer 中的错误 - 编辑问题以显示代码。

很可能您没有检测到流结束。

如果流在消息之间关闭,则需要抛出 SoftEndOfStreamException 来表明套接字已以“预期”方式关闭。如果流在消息反序列化期间关闭,则抛出一些其他异常。

关于java - Spring TCP 支持 CPU 利用率高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49402055/

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