0

我正在尝试使用编年史地图编写程序。我编写了一个 UDP 服务器,它将每 1 秒广播一条消息。UDP 客户端将接收消息并将消息存储在编年史地图中。程序如下:

UDP服务器程序:

public class UDPServer {

    public static void main(String[] args) {
        DatagramSocket socket = null;
        try {
            socket = new DatagramSocket();
            byte[] buf = new byte[256];
            String messg = "Hello UDP Server\n";
            String transmittedMsg = null;
            int count = 0;

            while (true) {
                transmittedMsg = count + "";
                buf = transmittedMsg.getBytes();
                InetAddress address = InetAddress.getByName ("127.0.0.1");
                DatagramPacket packet = new DatagramPacket (buf, buf.length, address, 4000);
                socket.send(packet);

                Thread.sleep(1000);
                count++;
            }
        } catch (SocketTimeoutException ex) {
            System.out.println("Timeout error: " + ex.getMessage());
            ex.printStackTrace();
        } catch (IOException ex) {
            System.out.println("Client error: " + ex.getMessage());
            ex.printStackTrace();
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        } finally {
            socket.close();
        }
    }
}

UDP客户端程序:

public class UDPClient {

    public static void main(String[] args) {
        DatagramSocket socket = null;
        DatagramPacket packet = null;
        byte[] buf = new byte[256];
        ChronicleMap<String, String> cr = null;

        try {
            socket = new DatagramSocket(4000);
            InetAddress address = InetAddress.getByName ("127.0.0.1");

            while (true) {
                packet = new DatagramPacket(buf, buf.length, address, 5000);
                socket.receive(packet);
                String received = new String(packet.getData());
                System.out.println(received);

                cr = ChronicleMapBuilder.of(String.class, String.class)
                        .name("test-map")
                        .averageKey("Message")
                        .averageValue("0")
                        .entries(1)
                        .actualChunkSize(100)
                        .actualSegments(1)
                        .createPersistedTo(new File("D://test.txt"));

                cr.put("Message", received);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cr != null) {
                cr.close();
            }
        }
    }
}

以下是我得到的例外:

java.lang.IllegalArgumentException: ChronicleMap{name=test-map, file=D:\test.txt, identityHashCode=11583403}: Entry is too large: requires 68 chunks, 9 is maximum.
    at net.openhft.chronicle.map.impl.CompiledMapQueryContext.allocReturnCode(CompiledMapQueryContext.java:1805)
    at net.openhft.chronicle.map.impl.CompiledMapQueryContext.allocReturnCodeGuarded(CompiledMapQueryContext.java:123)
    at net.openhft.chronicle.map.impl.CompiledMapQueryContext.alloc(CompiledMapQueryContext.java:3468)
    at net.openhft.chronicle.map.impl.CompiledMapQueryContext.initEntryAndKey(CompiledMapQueryContext.java:3502)
    at net.openhft.chronicle.map.impl.CompiledMapQueryContext.putEntry(CompiledMapQueryContext.java:3995)
    at net.openhft.chronicle.map.impl.CompiledMapQueryContext.doInsert(CompiledMapQueryContext.java:4184)
    at net.openhft.chronicle.map.MapEntryOperations.insert(MapEntryOperations.java:153)
    at net.openhft.chronicle.map.impl.CompiledMapQueryContext.insert(CompiledMapQueryContext.java:4107)
    at net.openhft.chronicle.map.MapMethods.put(MapMethods.java:88)
    at net.openhft.chronicle.map.VanillaChronicleMap.put(VanillaChronicleMap.java:724)
    at udp.client.UDPClient.main(UDPClient.java:38)

请帮忙。

4

1 回答 1

0

显然,您收到的某些条目比

averageKey("Message")
averageValue("0")

你指定的。

您还可以将高级配置:averageKey()averageValue()entries()和低级配置: actualChunkSize()、混合在一起actualSegments(),不建议这样做。

于 2019-10-18T12:18:49.467 回答