gpt4 book ai didi

Java多线程UDP服务器错误

转载 作者:行者123 更新时间:2023-12-02 06:37:27 25 4
gpt4 key购买 nike


我用Java编写UDP服务器和客户端,以将消息从客户端发送到服务器。就我而言,来自客户端的每条消息都将存储在数据库中。我尝试使用固定线程池制作多线程UDP服务器。但是现在我遇到了一个问题,我发现来自客户端的一条消息可以被多个线程处理。我的服务器看起来像这样:

    DatagramSocket serverSocket = new DatagramSocket(9876);
Map<String, Integer> retryMap = new ConcurrentHashMap<>();
byte[] receiveData = new byte[1024];

ExecutorService executor = Executors.newFixedThreadPool(2);

while(true){
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
executor.execute(new ThreadServer(receivePacket, retryMap));
}

ThreadServer类:

public void run(){
String temp = new String(packet.getData(), 0, packet.getLength());
long threadId = Thread.currentThread().getId();
System.out.println("Thread Id = "+threadId+" Message = "+temp);
// insert message to db
}

我的客户看起来像这样:

public class UDPClient1{
String message;
public UDPClient1(String message){
this.message = message;
try{
byte[] sendData;

DatagramSocket clientSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName("localhost");

sendData = message.getBytes("UTF-8");

DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
clientSocket.send(sendPacket);

clientSocket.close();
}catch(Exception e){
e.printStackTrace();
}
}

public static void main(String args[]) throws InterruptedException{
int i = 0;
while(i < 1000){
new UDPClient1("T000"+i+"_"+i);
System.out.println(i);
i++;
Thread.sleep(2);
}
}}

服务器的输出显示,有时来自客户端的一条消息将被 2 个或更多线程处理。

Thread Id = 18 Routing Id = T000975 09:54:11,759 ERROR LogRoutingFileDaoImpl:59 - AMS - Error insert to database org.springframework.dao.DuplicateKeyException: PreparedStatementCallback; SQL [INSERT INTO LOG_ROUTING_FILE (ID_ROUTING_FILE, FILE_NM , PATH, TYPES, PROTOCOL, SOURCE_NM, TARGET_NM) VALUES (?,?,?,?,?,?,?)]; ERROR: duplicate key value violates unique constraint "log_routing_file_id_routing_file_key"; nested exception is org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "log_routing_file_id_routing_file_key" 
Thread Id = 9 Routing Id = T000975 09:54:11,759 ERROR LogRoutingFileDaoImpl:59 - AMS - Error insert to database org.springframework.dao.DuplicateKeyException: PreparedStatementCallback; SQL [INSERT INTO LOG_ROUTING_FILE (ID_ROUTING_FILE, FILE_NM , PATH, TYPES, PROTOCOL, SOURCE_NM, TARGET_NM) VALUES (?,?,?,?,?,?,?)]; ERROR: duplicate key value violates unique constraint "log_routing_file_id_routing_file_key"; nested exception is org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "log_routing_file_id_routing_file_key"

消息 T000975 由线程 9 和 18 处理。有人能帮我解决这个问题吗? :)

最佳答案

byte[] receiveData = new byte[1024];

您在所有 DatagramPackets 之间共享相同的字节数组。将此行移至接收循环内。

关于Java多线程UDP服务器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19485613/

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