Java Code Examples for net.openhft.chronicle.core.io.Closeable#closeQuietly()

The following examples show how to use net.openhft.chronicle.core.io.Closeable#closeQuietly() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UberHandler.java    From Chronicle-Network with Apache License 2.0 6 votes vote down vote up
@Override
protected void performClose() {
    T nc = nc();
    if (!isClosing.getAndSet(true) && connectionChangedNotifier != null)
        connectionChangedNotifier.onConnectionChanged(false, nc);

    try {
        if (nc != null) {
            final ConnectionListener listener = nc.acquireConnectionListener();
            if (listener != null)
                listener.onDisconnected(localIdentifier, remoteIdentifier(), nc.isAcceptor());
        }
    } catch (Exception e) {
        Jvm.fatal().on(getClass(), "close:", e);
    }
    Closeable.closeQuietly(writers);
    super.performClose();
}
 
Example 2
Source File: TailerSequenceRaceConditionTest.java    From Chronicle-Queue with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldAlwaysBeAbleToTail() throws InterruptedException {
    ChronicleQueue[] queues = new ChronicleQueue[10];
    for (int i = 0; i < 10; i++) {
        final ChronicleQueue queue = createNewQueue();
        queues[i] = queue;
        for (int j = 0; j < 4; j++) {
            threadPool.submit(() -> attemptToMoveToTail(queue));
        }

        threadPool.submit(() -> appendToQueue(queue));

        for (int j = 0; j < 4; j++) {
            threadPool.submit(() -> attemptToMoveToTail(queue));
        }
    }

    threadPool.shutdown();
    assertTrue(threadPool.awaitTermination(5L, TimeUnit.SECONDS));
    assertFalse(failedToMoveToEnd.get());
    Closeable.closeQuietly(queues);
}
 
Example 3
Source File: StoreTailer.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@Override
protected void performClose() {
    Closeable.closeQuietly(indexValue);
    // the wire ref count will be released here by setting it to null
    context.wire(null);
    final Wire w0 = wireForIndex;
    if (w0 != null)
        w0.bytes().releaseLast();
    wireForIndex = null;
    releaseStore();
}
 
Example 4
Source File: SingleChronicleQueueStore.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@Override
protected void performClose() {
    Closeable.closeQuietly(writePosition);
    Closeable.closeQuietly(indexing);

    mappedBytes.release(INIT);
    try {
        mappedFile.release(this);
    } catch (IllegalStateException e) {
        e.printStackTrace();
    }
}
 
Example 5
Source File: HostConnector.java    From Chronicle-Network with Apache License 2.0 5 votes vote down vote up
@Override
protected synchronized void performClose() {
    WireOutPublisher wp = wireOutPublisher.getAndSet(null);

    ISocketChannel socketChannel = nc.socketChannel();
    if (socketChannel != null) {
        Closeable.closeQuietly(socketChannel);
        Closeable.closeQuietly(socketChannel.socket());
    }

    if (wp != null)
        wp.close();

}
 
Example 6
Source File: HeartbeatHandler.java    From Chronicle-Network with Apache License 2.0 5 votes vote down vote up
@Override
public void close() {
    if (connectionMonitor != null)
        connectionMonitor.onDisconnected(localIdentifier(), remoteIdentifier(), nc().isAcceptor());
    lastTimeMessageReceived = Long.MAX_VALUE;
    Closeable closable = closable();
    if (closable != null && !closable.isClosed()) {
        Closeable.closeQuietly(closable);
    }
}
 
Example 7
Source File: CspTcpHandler.java    From Chronicle-Network with Apache License 2.0 5 votes vote down vote up
protected void removeHandler(SubHandler<T> handler) {
    cidToHandle.remove(handler.cid());
    if (this.handler == handler) {
        this.handler = null;
        this.lastCid = 0;
    }
    Closeable.closeQuietly(handler);
}
 
Example 8
Source File: TerminatorHandler.java    From Chronicle-Network with Apache License 2.0 5 votes vote down vote up
@Override
public void onInitialize(@NotNull WireOut outWire) {
    if (isClosed.getAndSet(true))
        return;
    nc().terminationEventHandler().onTerminate(nc());
    Closeable.closeQuietly(closable());
}
 
Example 9
Source File: CspTcpHandler.java    From Chronicle-Network with Apache License 2.0 4 votes vote down vote up
@Override
protected void performClose() {
    Closeable.closeQuietly(cidToHandle.values());
    super.performClose();
}
 
Example 10
Source File: TableDirectoryListing.java    From Chronicle-Queue with Apache License 2.0 4 votes vote down vote up
protected void performClose() {
    Closeable.closeQuietly(minCycleValue, maxCycleValue, modCount);
}
 
Example 11
Source File: JDBCServiceTest.java    From Chronicle-Queue with Apache License 2.0 4 votes vote down vote up
private void doCreateTable(int repeats, int noUpdates) {
    for (int t = 0; t < repeats; t++) {
        long start = System.nanoTime(), written;
        File path1 = DirectoryUtils.tempDir("createTable1");
        File path2 = DirectoryUtils.tempDir("createTable2");
        File file = new File(OS.TARGET, "hsqldb-" + System.nanoTime());
        file.deleteOnExit();

        try (ChronicleQueue in = SingleChronicleQueueBuilder
                .binary(path1)
                .testBlockSize()
                .build();
             ChronicleQueue out = SingleChronicleQueueBuilder
                     .binary(path2)
                     .testBlockSize()
                     .build()) {

            JDBCService service = new JDBCService(in, out, () -> DriverManager.getConnection("jdbc:hsqldb:file:" + file.getAbsolutePath(), "SA", ""));

            JDBCStatement writer = service.createWriter();
            writer.executeUpdate("CREATE TABLE tableName (\n" +
                    "name VARCHAR(64) NOT NULL,\n" +
                    "num INT\n" +
                    ")\n");

            for (int i = 1; i < (long) noUpdates; i++)
                writer.executeUpdate("INSERT INTO tableName (name, num)\n" +
                        "VALUES (?, ?)", "name", i);

            written = System.nanoTime() - start;
            AtomicLong queries = new AtomicLong();
            AtomicLong updates = new AtomicLong();
            CountingJDBCResult countingJDBCResult = new CountingJDBCResult(queries, updates);
            MethodReader methodReader = service.createReader(countingJDBCResult);
            while (updates.get() < noUpdates) {
                if (!methodReader.readOne())
                    Thread.yield();
            }
            Closeable.closeQuietly(service);

            long time = System.nanoTime() - start;
            System.out.printf("Average time to write each update %.1f us, average time to perform each update %.1f us%n",
                    written / noUpdates / 1e3,
                    time / noUpdates / 1e3);
        } finally {
            try {
                IOTools.deleteDirWithFiles(path1, 2);
                IOTools.deleteDirWithFiles(path2, 2);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
 
Example 12
Source File: ServiceWrapperBuilder.java    From Chronicle-Queue with Apache License 2.0 4 votes vote down vote up
public void closeQueues() {
    Closeable.closeQuietly(queues);
}
 
Example 13
Source File: NioSslIntegrationTest.java    From Chronicle-Network with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldEncryptAndDecryptTraffic() throws Exception {
    final ExecutorService threadPool = Executors.newFixedThreadPool(2,
            new NamedThreadFactory("test"));

    final ServerSocketChannel serverChannel = ServerSocketChannel.open();
    serverChannel.bind(new InetSocketAddress("0.0.0.0", 13337));
    serverChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
    serverChannel.configureBlocking(true);

    final SocketChannel channel = SocketChannel.open();
    channel.configureBlocking(false);
    channel.connect(new InetSocketAddress("127.0.0.1", serverChannel.socket().getLocalPort()));

    try {

        final Client client = new Client(channel);

        final StateMachineProcessor clientProcessor = new StateMachineProcessor(channel, false,
                SSLContextLoader.getInitialisedContext(), client);

        final SocketChannel serverConnection = serverChannel.accept();
        serverConnection.configureBlocking(false);

        final Server server = new Server(serverConnection);
        final StateMachineProcessor serverProcessor = new StateMachineProcessor(serverConnection, true,
                SSLContextLoader.getInitialisedContext(), server);

        while (!(channel.finishConnect() && serverConnection.finishConnect())) {
            Thread.yield();
        }

        if (SEND_DATA_BEFORE_SSL_HANDSHAKE) {
            testDataConnection(channel, serverConnection);
        }

        threadPool.submit(clientProcessor);
        threadPool.submit(serverProcessor);

        client.waitForResponse(10, TimeUnit.SECONDS);
        serverProcessor.stop();
        clientProcessor.stop();
    } finally {
        Closeable.closeQuietly(channel, serverChannel);
    }

    threadPool.shutdown();
    assertTrue(threadPool.awaitTermination(10, TimeUnit.SECONDS));
}
 
Example 14
Source File: Cluster.java    From Chronicle-Network with Apache License 2.0 4 votes vote down vote up
@Override
protected void performClose() {
    Closeable.closeQuietly(hostDetails());
}
 
Example 15
Source File: TcpChannelHub.java    From Chronicle-Network with Apache License 2.0 4 votes vote down vote up
/**
 * you are unlikely to want to call this method in a production environment the purpose of this method is to simulate a network outage
 */
public void forceDisconnect() {
    Closeable.closeQuietly(clientChannel);
}
 
Example 16
Source File: TcpChannelHub.java    From Chronicle-Network with Apache License 2.0 4 votes vote down vote up
@Nullable
SocketChannel openSocketChannel(final InetSocketAddress socketAddress) throws IOException {
    final SocketChannel result = SocketChannel.open();
    @Nullable Selector selector = null;
    boolean failed = true;
    try {
        result.configureBlocking(false);
        Socket socket = result.socket();
        socket.setTcpNoDelay(true);
        socket.setReceiveBufferSize(tcpBufferSize);
        socket.setSendBufferSize(tcpBufferSize);
        socket.setSoTimeout(0);
        socket.setSoLinger(false, 0);
        result.connect(socketAddress);

        selector = Selector.open();
        result.register(selector, SelectionKey.OP_CONNECT);

        int select = selector.select(2500);
        if (select == 0) {
            Jvm.warn().on(TcpChannelHub.class, "Timed out attempting to connect to " + socketAddress);
            return null;
        } else {
            try {
                if (!result.finishConnect())
                    return null;

            } catch (IOException e) {
                if (DEBUG_ENABLED)
                    Jvm.debug().on(TcpChannelHub.class, "Failed to connect to " + socketAddress + " " + e);
                return null;
            }
        }
        failed = false;
        return result;

    } finally {
        Closeable.closeQuietly(selector);
        if (failed)
            Closeable.closeQuietly(result);
    }
}
 
Example 17
Source File: AbstractTSQueueLock.java    From Chronicle-Queue with Apache License 2.0 4 votes vote down vote up
protected void performClose() {
    Closeable.closeQuietly(lock);
}
 
Example 18
Source File: VanillaNetworkContext.java    From Chronicle-Network with Apache License 2.0 4 votes vote down vote up
@Override
protected void performClose() {
    Closeable.closeQuietly(networkStatsListener);
}
 
Example 19
Source File: WireTcpHandler.java    From Chronicle-Network with Apache License 2.0 4 votes vote down vote up
@Override
protected void performClose() {
    Closeable.closeQuietly(nc);
}
 
Example 20
Source File: RemoteConnector.java    From Chronicle-Network with Apache License 2.0 4 votes vote down vote up
@Override
protected void performClose() {
    Closeable.closeQuietly(closeables);
}