net.openhft.chronicle.core.io.Closeable Java Examples
The following examples show how to use
net.openhft.chronicle.core.io.Closeable.
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: SingleCQFormatTest.java From Chronicle-Queue with Apache License 2.0 | 6 votes |
@Test(expected = TimeoutException.class) public void testDeadHeader() throws IOException { @NotNull File dir = DirectoryUtils.tempDir("testDeadHeader"); dir.mkdirs(); File file = new File(dir, "19700101" + SingleChronicleQueue.SUFFIX); file.createNewFile(); @NotNull MappedBytes bytes = MappedBytes.mappedBytes(file, ChronicleQueue.TEST_BLOCK_SIZE); bytes.writeInt(Wires.NOT_COMPLETE | Wires.META_DATA); bytes.releaseLast(); @Nullable ChronicleQueue queue = null; try { queue = binary(dir).timeoutMS(500L) .testBlockSize() .blockSize(ChronicleQueue.TEST_BLOCK_SIZE) .build(); testQueue(queue); } finally { Closeable.closeQuietly(queue); IOTools.shallowDeleteDirWithFiles(dir.getAbsolutePath()); } }
Example #2
Source File: TailerSequenceRaceConditionTest.java From Chronicle-Queue with Apache License 2.0 | 6 votes |
@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: TcpEventHandler.java From Chronicle-Network with Apache License 2.0 | 6 votes |
@Override protected void performClose() { Closeable.closeQuietly(tcpHandler); Closeable.closeQuietly(this.nc.networkStatsListener()); Closeable.closeQuietly(sc); Closeable.closeQuietly(nc); // NOTE Do not release buffers here as they might be in use. loopFinished() releases them and // is called from event loop when it knows that the thread calling "action" is done if (Thread.currentThread() == actionThread) return; for (int i = 50; i >= 0; i--) { if (inBBB.refCount() + outBBB.refCount() == 0) break; Jvm.pause(2); if (i == 0) { Throwable thrown = actionThread == null ? null : StackTrace.forThread(actionThread); Jvm.warn().on(getClass(), "loopFinished failed to release buffers", thrown); } } }
Example #4
Source File: SingleChronicleQueue.java From Chronicle-Queue with Apache License 2.0 | 6 votes |
@SuppressWarnings("unchecked") @Override protected void performClose() { synchronized (closers) { closers.forEach(Closeable::closeQuietly); closers.clear(); } // must be closed after closers. closeQuietly(directoryListing, queueLock, lastAcknowledgedIndexReplicated, lastIndexReplicated, writeLock); this.pool.close(); this.storeSupplier.close(); closeQuietly(metaStore); // close it if we created it. if (eventLoop instanceof OnDemandEventLoop) eventLoop.close(); }
Example #5
Source File: EventLoopServiceWrapper.java From Chronicle-Queue with Apache License 2.0 | 6 votes |
@Override public boolean action() throws InvalidEventHandlerException { if (isClosed()) { Closeable.closeQuietly(serviceImpl); Closeable.closeQuietly(serviceIn); Closeable.closeQuietly(outputQueue); Closeable.closeQuietly(inputQueues); throw new InvalidEventHandlerException(); } boolean busy = false; for (MethodReader reader : serviceIn) { busy |= reader.readOne(); } return busy; }
Example #6
Source File: UberHandler.java From Chronicle-Network with Apache License 2.0 | 6 votes |
@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 #7
Source File: HeartbeatHandler.java From Chronicle-Network with Apache License 2.0 | 5 votes |
@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 #8
Source File: WordCountTest.java From Chronicle-Map with Apache License 2.0 | 5 votes |
@Test public void wordCountTest() throws IOException { try (ChronicleMap<CharSequence, IntValue> map = ChronicleMap .of(CharSequence.class, IntValue.class) .averageKeySize(7) // average word is 7 ascii bytes long (text in english) .entries(expectedSize) .create()) { IntValue v = Values.newNativeReference(IntValue.class); for (String word : words) { try (Closeable ignored = map.acquireContext(word, v)) { v.addValue(1); } } } }
Example #9
Source File: ChronicleMapImportExportTest.java From Chronicle-Map with Apache License 2.0 | 5 votes |
@Test public void testBondVOInterface() throws IOException, InterruptedException { File file = new File(TMP + "/chronicle-map-" + System.nanoTime() + ".json"); file.deleteOnExit(); System.out.println(file.getAbsolutePath()); ChronicleMapBuilder<CharSequence, BondVOInterface> builder = ChronicleMapBuilder.of(CharSequence.class, BondVOInterface.class) .averageKeySize("one".length()).entries(1000); try (ChronicleMap<CharSequence, BondVOInterface> expected = builder.create()) { final BondVOInterface value = Values.newNativeReference(BondVOInterface.class); // this will add the entry try (Closeable c = expected.acquireContext("one", value)) { value.setCoupon(8.98); BondVOInterface.MarketPx marketPxIntraDayHistoryAt = value.getMarketPxIntraDayHistoryAt(1); marketPxIntraDayHistoryAt.setAskPx(12.0); } expected.getAll(file); try (ChronicleMap<CharSequence, BondVOInterface> actual = builder.create()) { actual.putAll(file); Assert.assertEquals(expected.get("one").getCoupon(), actual.get("one").getCoupon(), 0); } } finally { file.delete(); } }
Example #10
Source File: ChronicleMapImportExportTest.java From Chronicle-Map with Apache License 2.0 | 5 votes |
@Test public void testWithLongValue() throws IOException, InterruptedException { File file = new File(TMP + "/chronicle-map-" + System.nanoTime() + ".json"); //file.deleteOnExit(); System.out.println(file.getAbsolutePath()); ChronicleMapBuilder<CharSequence, LongValue> builder = ChronicleMapBuilder .of(CharSequence.class, LongValue.class) .averageKeySize("one".length()) .entries(1000); try (ChronicleMap<CharSequence, LongValue> expected = builder.create()) { LongValue value = Values.newNativeReference(LongValue.class); // this will add the entry try (Closeable c = expected.acquireContext("one", value)) { assertEquals(0, value.getValue()); value.addValue(1); } expected.getAll(file); try (ChronicleMap<CharSequence, LongValue> actual = builder.create()) { actual.putAll(file); Assert.assertEquals(expected, actual); } } finally { // file.delete(); } }
Example #11
Source File: SingleChronicleQueueStore.java From Chronicle-Queue with Apache License 2.0 | 5 votes |
@Override protected void performClose() { Closeable.closeQuietly(writePosition); Closeable.closeQuietly(indexing); mappedBytes.release(INIT); try { mappedFile.release(this); } catch (IllegalStateException e) { e.printStackTrace(); } }
Example #12
Source File: TerminatorHandler.java From Chronicle-Network with Apache License 2.0 | 5 votes |
@Override public void onInitialize(@NotNull WireOut outWire) { if (isClosed.getAndSet(true)) return; nc().terminationEventHandler().onTerminate(nc()); Closeable.closeQuietly(closable()); }
Example #13
Source File: StoreTailer.java From Chronicle-Queue with Apache License 2.0 | 5 votes |
@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 #14
Source File: CspTcpHandler.java From Chronicle-Network with Apache License 2.0 | 5 votes |
protected void removeHandler(SubHandler<T> handler) { cidToHandle.remove(handler.cid()); if (this.handler == handler) { this.handler = null; this.lastCid = 0; } Closeable.closeQuietly(handler); }
Example #15
Source File: SingleChronicleQueue.java From Chronicle-Queue with Apache License 2.0 | 5 votes |
public <T> void addCloseListener(Closeable key) { synchronized (closers) { if (!closers.isEmpty()) closers.removeIf(Closeable::isClosed); closers.add(key); } }
Example #16
Source File: HostConnector.java From Chronicle-Network with Apache License 2.0 | 5 votes |
@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 #17
Source File: NioSslIntegrationTest.java From Chronicle-Network with Apache License 2.0 | 4 votes |
@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 #18
Source File: TableDirectoryListing.java From Chronicle-Queue with Apache License 2.0 | 4 votes |
protected void performClose() { Closeable.closeQuietly(minCycleValue, maxCycleValue, modCount); }
Example #19
Source File: AbstractTSQueueLock.java From Chronicle-Queue with Apache License 2.0 | 4 votes |
protected void performClose() { Closeable.closeQuietly(lock); }
Example #20
Source File: ServiceWrapperBuilder.java From Chronicle-Queue with Apache License 2.0 | 4 votes |
public void closeQueues() { Closeable.closeQuietly(queues); }
Example #21
Source File: JDBCServiceTest.java From Chronicle-Queue with Apache License 2.0 | 4 votes |
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 #22
Source File: ReplicationCheckingMap.java From Chronicle-Map with Apache License 2.0 | 4 votes |
@NotNull @Override public Closeable acquireContext( @NotNull K key, @NotNull V usingValue) { throw new UnsupportedOperationException(); }
Example #23
Source File: RemoteConnector.java From Chronicle-Network with Apache License 2.0 | 4 votes |
private static void closeSocket(SocketChannel socketChannel) { Closeable.closeQuietly(socketChannel); }
Example #24
Source File: Cluster.java From Chronicle-Network with Apache License 2.0 | 4 votes |
@Override protected void performClose() { Closeable.closeQuietly(hostDetails()); }
Example #25
Source File: AbstractSubHandler.java From Chronicle-Network with Apache License 2.0 | 4 votes |
@Override public Closeable closable() { return closeable; }
Example #26
Source File: CspTcpHandler.java From Chronicle-Network with Apache License 2.0 | 4 votes |
@Override protected void performClose() { Closeable.closeQuietly(cidToHandle.values()); super.performClose(); }
Example #27
Source File: TcpChannelHub.java From Chronicle-Network with Apache License 2.0 | 4 votes |
/** * 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 #28
Source File: TcpChannelHub.java From Chronicle-Network with Apache License 2.0 | 4 votes |
@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 #29
Source File: VanillaNetworkContext.java From Chronicle-Network with Apache License 2.0 | 4 votes |
@Override protected void performClose() { Closeable.closeQuietly(networkStatsListener); }
Example #30
Source File: WireTcpHandler.java From Chronicle-Network with Apache License 2.0 | 4 votes |
@Override protected void performClose() { Closeable.closeQuietly(nc); }