private static Set<ServiceDefinition> parse(URLDefinition urlDefinition) { try { final Set<ServiceDefinition> names = new HashSet<ServiceDefinition>(); BufferedReader r = null; try { URL url = urlDefinition.uri.toURL(); r = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8")); while (true) { String line = r.readLine(); if (line == null) { break; } int comment = line.indexOf('#'); if (comment >= 0) { line = line.substring(0, comment); } String name = line.trim(); if (name.length() == 0) { continue; } names.add(new ServiceDefinition(name, urlDefinition.classLoader)); } } finally { IOUtil.closeResource(r); } return names; } catch (Exception e) { LOGGER.severe(e); } return Collections.emptySet(); }
@Override public synchronized void stop() { if (!live) { return; } live = false; logger.finest("Stopping ConnectionManager"); if (acceptorThread != null) { acceptorThread.shutdown(); } for (SocketChannelWrapper socketChannel : acceptedSockets) { closeResource(socketChannel); } for (Connection conn : connectionsMap.values()) { destroySilently(conn); } for (TcpIpConnection conn : activeConnections) { destroySilently(conn); } ioThreadingModel.shutdown(); acceptedSockets.clear(); connectionsInProgress.clear(); connectionsMap.clear(); monitors.clear(); activeConnections.clear(); }
public void run() { while (!Thread.currentThread().isInterrupted()) { try { if (conn == null) { try { conn = pickConnection(); } catch (Exception e) { logger.severe("Error while connecting to cluster!", e); client.getLifecycleService().shutdown(); return; } } loadInitialMemberList(); listenMembershipEvents(); } catch (Exception e) { if (client.getLifecycleService().isRunning()) { if (logger.isFinestEnabled()) { logger.warning("Error while listening cluster events! -> " + conn, e); } else { logger.warning( "Error while listening cluster events! -> " + conn + ", Error: " + e.toString()); } } IOUtil.closeResource(conn); conn = null; fireConnectionEvent(true); } try { Thread.sleep(1000); } catch (InterruptedException e) { break; } } }
private static <C extends Closeable> void offerOrClose(Queue<C> queue, C item) { if (queue.size() == MAX_POOLED_ITEMS) { closeResource(item); return; } queue.offer(item); }
private void deleteCacheResources(String name) { Set<Closeable> cacheResources = resources.remove(name); if (cacheResources != null) { for (Closeable resource : cacheResources) { IOUtil.closeResource(resource); } cacheResources.clear(); } }
private void close() { final ClientConnection currentOwnerConnection = ownerConnection; if (currentOwnerConnection == null) { return; } IOUtil.closeResource(currentOwnerConnection); markAsClosed(); }
@Override public boolean deregisterListener(String name, String registrationId) { final EventService eventService = getNodeEngine().getEventService(); boolean result = eventService.deregisterListener(SERVICE_NAME, name, registrationId); Closeable listener = closeableListeners.remove(registrationId); if (listener != null) { IOUtil.closeResource(listener); } return result; }
public static Object serializeToObject(byte[] bytes) throws Exception { if (bytes == null) return null; ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes)); try { Object obj = in.readObject(); return obj; } finally { closeResource(in); } }
public static byte[] serializeToBytes(Object object) throws Exception { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(bos); try { out.writeObject(object); return bos.toByteArray(); } finally { closeResource(out); } }
public Map<String, String> phoneHome(Node hazelcastNode, String version, boolean isEnterprise) { String downloadId = "source"; InputStream is = null; try { is = getClass().getClassLoader().getResourceAsStream("hazelcast-download.properties"); if (is != null) { final Properties properties = new Properties(); properties.load(is); downloadId = properties.getProperty("hazelcastDownloadId"); } } catch (IOException ignored) { EmptyStatement.ignore(ignored); } finally { IOUtil.closeResource(is); } // Calculate native memory usage from native memory config NativeMemoryConfig memoryConfig = hazelcastNode.getConfig().getNativeMemoryConfig(); final ClusterServiceImpl clusterService = hazelcastNode.getClusterService(); long totalNativeMemorySize = clusterService.getSize(DATA_MEMBER_SELECTOR) * memoryConfig.getSize().bytes(); String nativeMemoryParameter = (isEnterprise) ? Long.toString(MemoryUnit.BYTES.toGigaBytes(totalNativeMemorySize)) : "0"; // Calculate connected clients to the cluster. Map<ClientType, Integer> clusterClientStats = hazelcastNode.clientEngine.getConnectedClientStats(); RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean(); Long clusterUpTime = clusterService.getClusterClock().getClusterUpTime(); PhoneHomeParameterCreator parameterCreator = new PhoneHomeParameterCreator(); parameterCreator.addParam("version", version); parameterCreator.addParam("m", hazelcastNode.getLocalMember().getUuid()); parameterCreator.addParam("e", Boolean.toString(isEnterprise)); parameterCreator.addParam("l", MD5Util.toMD5String(hazelcastNode.getConfig().getLicenseKey())); parameterCreator.addParam("p", downloadId); parameterCreator.addParam("c", clusterService.getClusterId()); parameterCreator.addParam("crsz", convertToLetter(clusterService.getMembers().size())); parameterCreator.addParam( "cssz", convertToLetter(hazelcastNode.clientEngine.getClientEndpointCount())); parameterCreator.addParam("hdgb", nativeMemoryParameter); parameterCreator.addParam("ccpp", Integer.toString(clusterClientStats.get(ClientType.CPP))); parameterCreator.addParam("cdn", Integer.toString(clusterClientStats.get(ClientType.CSHARP))); parameterCreator.addParam("cjv", Integer.toString(clusterClientStats.get(ClientType.JAVA))); parameterCreator.addParam("cuptm", Long.toString(clusterUpTime)); parameterCreator.addParam("nuptm", Long.toString(runtimeMxBean.getUptime())); String urlStr = BASE_PHONE_HOME_URL + parameterCreator.build(); fetchWebService(urlStr); return parameterCreator.getParameters(); }
public void destroy() { active = false; for (SerializerAdapter serializer : typeMap.values()) { serializer.destroy(); } typeMap.clear(); idMap.clear(); global.set(null); constantTypesMap.clear(); for (BufferObjectDataOutput output : outputPool) { IOUtil.closeResource(output); } outputPool.clear(); }
@Override public void onDetectingUnresponsiveConnection(ClientConnection connection) { if (smartRouting) { // closing the owner connection if unresponsive so that it can be switched to a healthy one. ownerConnectionFuture.closeIfAddressMatches(connection.getEndPoint()); // we do not close connection itself since we will continue to send heartbeat ping to this // connection. // IOUtil.closeResource(connection); return; } // close both owner and operation connection ownerConnectionFuture.close(); IOUtil.closeResource(connection); }
private void fetchWebService(String urlStr) { InputStream in = null; try { URL url = new URL(urlStr); URLConnection conn = url.openConnection(); conn.setRequestProperty("User-Agent", "Mozilla/5.0"); conn.setConnectTimeout(TIMEOUT * 2); conn.setReadTimeout(TIMEOUT * 2); in = new BufferedInputStream(conn.getInputStream()); } catch (IOException ignored) { EmptyStatement.ignore(ignored); } finally { IOUtil.closeResource(in); } }
private void _sendAndHandle( ConnectionFactory connectionFactory, Object obj, ResponseHandler handler) throws IOException { ResponseStream stream = null; while (stream == null) { if (!active) { throw new HazelcastInstanceNotActiveException(); } Connection conn = null; try { conn = connectionFactory.create(); final SerializationService serializationService = getSerializationService(); final Data request = serializationService.toData(obj); conn.write(request); stream = new ResponseStreamImpl(serializationService, conn); } catch (Exception e) { if (e instanceof IOException) { if (logger.isFinestEnabled()) { logger.finest("Error on connection... conn: " + conn + ", error: " + e); } } if (conn != null) { IOUtil.closeResource(conn); } if (ErrorHandler.isRetryable(e)) { if (redoOperation || obj instanceof RetryableRequest) { if (logger.isFinestEnabled()) { logger.finest("Retrying " + obj + ", last-conn: " + conn + ", last-error: " + e); } beforeRetry(); continue; } } if (e instanceof IOException && !active) { continue; } throw ExceptionUtil.rethrow(e, IOException.class); } } try { handler.handle(stream); } catch (Exception e) { throw ExceptionUtil.rethrow(e, IOException.class); } finally { stream.end(); } }
private <T> T _sendAndReceive(ConnectionFactory connectionFactory, Object obj) throws IOException { while (active) { Connection conn = null; boolean release = true; try { conn = connectionFactory.create(); final SerializationService serializationService = getSerializationService(); final Data request = serializationService.toData(obj); conn.write(request); final Data response = conn.read(); final Object result = serializationService.toObject(response); return ErrorHandler.returnResultOrThrowException(result); } catch (Exception e) { if (e instanceof IOException) { if (logger.isFinestEnabled()) { logger.finest("Error on connection... conn: " + conn + ", error: " + e); } IOUtil.closeResource(conn); release = false; } if (ErrorHandler.isRetryable(e)) { if (redoOperation || obj instanceof RetryableRequest) { if (logger.isFinestEnabled()) { logger.finest("Retrying " + obj + ", last-conn: " + conn + ", last-error: " + e); } beforeRetry(); continue; } } if (e instanceof IOException && !active) { continue; } throw ExceptionUtil.rethrow(e, IOException.class); } finally { if (release && conn != null) { conn.release(); } } } throw new HazelcastInstanceNotActiveException(); }
@Override public void deregisterAllListener(String name) { final EventService eventService = getNodeEngine().getEventService(); final Collection<EventRegistration> registrations = eventService.getRegistrations(SERVICE_NAME, name); if (registrations != null) { for (EventRegistration registration : registrations) { Closeable listener = closeableListeners.remove(registration.getId()); if (listener != null) { IOUtil.closeResource(listener); } } } eventService.deregisterAllListeners(AbstractCacheService.SERVICE_NAME, name); CacheContext cacheContext = cacheContexts.get(name); if (cacheContext != null) { cacheContext.resetCacheEntryListenerCount(); cacheContext.resetInvalidationListenerCount(); } }
public void run() { while (!Thread.currentThread().isInterrupted()) { try { if (conn == null) { try { conn = connectToOne(); } catch (Exception e) { LOGGER.severe("Error while connecting to cluster!", e); client.getLifecycleService().shutdown(); latch.countDown(); return; } } getInvocationService().triggerFailedListeners(); loadInitialMemberList(); listenMembershipEvents(); } catch (Exception e) { if (client.getLifecycleService().isRunning()) { if (LOGGER.isFinestEnabled()) { LOGGER.warning("Error while listening cluster events! -> " + conn, e); } else { LOGGER.warning( "Error while listening cluster events! -> " + conn + ", Error: " + e.toString()); } } connectionManager.markOwnerConnectionAsClosed(); IOUtil.closeResource(conn); conn = null; clusterService.fireConnectionEvent(true); } try { Thread.sleep(SLEEP_TIME); } catch (InterruptedException e) { latch.countDown(); break; } } }
@Override protected Class<?> findClass(String name) throws ClassNotFoundException { if (!wellKnownClasses.contains(name)) { return super.findClass(name); } String path = name.replace('.', '/') + ".class"; InputStream in = null; try { in = getParent().getResourceAsStream(path); ByteArrayOutputStream bout = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; int read; while ((read = in.read(buf)) != -1) { bout.write(buf, 0, read); } byte[] code = bout.toByteArray(); return defineClass(name, code, 0, code.length); } catch (IOException e) { return super.findClass(name); } finally { IOUtil.closeResource(in); } }
public void close() { logger.info("Closing connection -> " + connection); IOUtil.closeResource(connection); }
protected void closeCacheLoader() { // close the configured CacheLoader if (cacheLoader instanceof Closeable) { IOUtil.closeResource((Closeable) cacheLoader); } }