public <T> T sendAndReceiveFixedConnection(Connection conn, Object obj) throws IOException { 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); }
void shutdown() { interrupt(); final Connection c = conn; if (c != null) { try { c.close(); } catch (IOException e) { logger.warning("Error while closing connection!", e); } } }
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(); }
private Object authenticate( Connection connection, Credentials credentials, ClientPrincipal principal, boolean reAuth, boolean firstConnection) throws IOException { AuthenticationRequest auth = new AuthenticationRequest(credentials, principal); auth.setReAuth(reAuth); auth.setFirstConnection(firstConnection); final SerializationService serializationService = getSerializationService(); connection.write(serializationService.toData(auth)); final Data addressData = connection.read(); Address address = ErrorHandler.returnResultOrThrowException(serializationService.toObject(addressData)); connection.setRemoteEndpoint(address); final Data data = connection.read(); return ErrorHandler.returnResultOrThrowException(serializationService.toObject(data)); }
private void loadInitialMemberList() throws IOException { final SerializationService serializationService = getSerializationService(); final Data request = serializationService.toData(new AddMembershipListenerRequest()); conn.write(request); final Data response = conn.read(); SerializableCollection coll = ErrorHandler.returnResultOrThrowException(serializationService.toObject(response)); Map<String, MemberImpl> prevMembers = Collections.emptyMap(); if (!members.isEmpty()) { prevMembers = new HashMap<String, MemberImpl>(members.size()); for (MemberImpl member : members) { prevMembers.put(member.getUuid(), member); } members.clear(); } for (Data d : coll.getCollection()) { members.add((MemberImpl) serializationService.toObject(d)); } updateMembersRef(); logger.info(membersString()); final List<MembershipEvent> events = new LinkedList<MembershipEvent>(); final Set<Member> eventMembers = Collections.unmodifiableSet(new LinkedHashSet<Member>(members)); for (MemberImpl member : members) { final MemberImpl former = prevMembers.remove(member.getUuid()); if (former == null) { events.add( new MembershipEvent( client.getCluster(), member, MembershipEvent.MEMBER_ADDED, eventMembers)); } } for (MemberImpl member : prevMembers.values()) { events.add( new MembershipEvent( client.getCluster(), member, MembershipEvent.MEMBER_REMOVED, eventMembers)); } for (MembershipEvent event : events) { fireMembershipEvent(event); } }
private void listenMembershipEvents() throws IOException { final SerializationService serializationService = getSerializationService(); while (!Thread.currentThread().isInterrupted()) { final Data eventData = conn.read(); final ClientMembershipEvent event = (ClientMembershipEvent) serializationService.toObject(eventData); final MemberImpl member = (MemberImpl) event.getMember(); if (event.getEventType() == MembershipEvent.MEMBER_ADDED) { members.add(member); } else { members.remove(member); getConnectionManager().removeConnectionPool(member.getAddress()); } updateMembersRef(); logger.info(membersString()); fireMembershipEvent( new MembershipEvent( client.getCluster(), member, event.getEventType(), Collections.unmodifiableSet(new LinkedHashSet<Member>(members)))); } }
public Client getLocalClient() { ClientPrincipal cp = principal; Connection conn = clusterThread.conn; return new ClientImpl( cp != null ? cp.getUuid() : null, conn != null ? conn.getLocalSocketAddress() : null); }