public static void removeStripe(String stripeName) { StripeDescriptor stripeDescriptor = STRIPES.remove(stripeName); for (Connection connection : stripeDescriptor.getConnections()) { try { LOGGER.warn("Force close {}", formatConnectionId(connection)); connection.close(); } catch (IllegalStateException e) { // Ignored in case connection is already closed } catch (IOException e) { // Ignored } } stripeDescriptor.removeConnections(); }
/** * Removes the {@link PassthroughServer} previously associated with the {@code URI} provided. The * server is stopped as it is removed. In addition to stopping the server, all open {@link * Connection} instances created through the {@link #connect(URI, Properties)} method are closed; * this is done to clean up the threads started in support of each open connection. * * @param uri the {@code URI} for which the server is removed * @return the removed {@code PassthroughServer} */ public static PassthroughServer remove(URI uri) { URI keyURI = createKey(uri); ServerDescriptor serverDescriptor = SERVERS.remove(keyURI); if (serverDescriptor != null) { for (Connection connection : serverDescriptor.getConnections().keySet()) { try { LOGGER.warn("Force close {}", formatConnectionId(connection)); connection.close(); } catch (AssertionError e) { // Ignored -- https://github.com/Terracotta-OSS/terracotta-apis/issues/102 } catch (IOException e) { // Ignored } } // open destroy connection. You need to make sure connection doesn't have any entities // associated with it. PassthroughConnection connection = serverDescriptor.server.connectNewClient("destroy-connection"); for (Entry entry : serverDescriptor.knownEntities.entrySet()) { @SuppressWarnings("unchecked") Class<? extends Entity> type = (Class) entry.getKey(); List args = (List) entry.getValue(); Long version = (Long) args.get(0); String stringArg = (String) args.get(1); try { EntityRef entityRef = connection.getEntityRef(type, version, stringArg); entityRef.destroy(); } catch (EntityNotProvidedException ex) { LOGGER.error("Entity destroy failed: ", ex); } catch (EntityNotFoundException ex) { LOGGER.error("Entity destroy failed: ", ex); } catch (PermanentEntityException ex) { LOGGER.error("Entity destroy failed (permanent???): ", ex); } } serverDescriptor.server.stop(); LOGGER.info("Stopped PassthroughServer at {}", keyURI); return serverDescriptor.server; } else { return null; } }
private static CharSequence formatConnectionId(Connection connection) { return connection.getClass().getSimpleName() + "@" + Integer.toHexString(System.identityHashCode(connection)); }