Search in sources :

Example 56 with Connection

use of com.hazelcast.nio.Connection in project hazelcast by hazelcast.

the class GroupMismatchOperation method run.

@Override
public void run() {
    NodeEngineImpl nodeEngine = (NodeEngineImpl) getNodeEngine();
    Connection connection = getConnection();
    String message = "Node could not join cluster at node: " + connection.getEndPoint() + " Cause: the target cluster has a different group-name";
    connection.close(message, null);
    ILogger logger = nodeEngine.getLogger("com.hazelcast.cluster");
    logger.warning(message);
    Node node = nodeEngine.getNode();
    node.getJoiner().blacklist(getCallerAddress(), true);
}
Also used : NodeEngineImpl(com.hazelcast.spi.impl.NodeEngineImpl) Node(com.hazelcast.instance.Node) Connection(com.hazelcast.nio.Connection) ILogger(com.hazelcast.logging.ILogger)

Example 57 with Connection

use of com.hazelcast.nio.Connection in project hazelcast by hazelcast.

the class OperationServiceImpl method send.

@Override
public boolean send(Operation op, Address target) {
    checkNotNull(target, "Target is required!");
    if (thisAddress.equals(target)) {
        throw new IllegalArgumentException("Target is this node! -> " + target + ", op: " + op);
    }
    byte[] bytes = serializationService.toBytes(op);
    int partitionId = op.getPartitionId();
    Packet packet = new Packet(bytes, partitionId).setPacketType(Packet.Type.OPERATION);
    if (op.isUrgent()) {
        packet.raiseFlags(FLAG_URGENT);
    }
    ConnectionManager connectionManager = node.getConnectionManager();
    Connection connection = connectionManager.getOrConnect(target);
    return connectionManager.transmit(packet, connection);
}
Also used : Packet(com.hazelcast.nio.Packet) ConnectionManager(com.hazelcast.nio.ConnectionManager) Connection(com.hazelcast.nio.Connection)

Example 58 with Connection

use of com.hazelcast.nio.Connection in project hazelcast by hazelcast.

the class OutboundResponseHandler method sendResponse.

@Override
public void sendResponse(Operation operation, Object obj) {
    Response response = toResponse(operation, obj);
    if (!send(response, operation.getCallerAddress())) {
        Connection conn = operation.getConnection();
        logger.warning("Cannot send response: " + obj + " to " + conn.getEndPoint() + ". " + operation);
    }
}
Also used : Response(com.hazelcast.spi.impl.operationservice.impl.responses.Response) NormalResponse(com.hazelcast.spi.impl.operationservice.impl.responses.NormalResponse) ErrorResponse(com.hazelcast.spi.impl.operationservice.impl.responses.ErrorResponse) Connection(com.hazelcast.nio.Connection)

Example 59 with Connection

use of com.hazelcast.nio.Connection in project hazelcast by hazelcast.

the class ClientHeartbeatTest method testAddingListenerToNewConnectionFailedBecauseOfHeartbeat.

@Test
public void testAddingListenerToNewConnectionFailedBecauseOfHeartbeat() throws Exception {
    hazelcastFactory.newHazelcastInstance();
    ClientConfig clientConfig = new ClientConfig();
    clientConfig.setProperty(ClientProperty.HEARTBEAT_TIMEOUT.getName(), "4000");
    clientConfig.setProperty(ClientProperty.HEARTBEAT_INTERVAL.getName(), "1000");
    final HazelcastInstance client = hazelcastFactory.newHazelcastClient(clientConfig);
    HazelcastClientInstanceImpl clientInstanceImpl = getHazelcastClientInstanceImpl(client);
    final ClientListenerService clientListenerService = clientInstanceImpl.getListenerService();
    final CountDownLatch blockIncoming = new CountDownLatch(1);
    final CountDownLatch heartbeatStopped = new CountDownLatch(1);
    final CountDownLatch onListenerRegister = new CountDownLatch(2);
    clientInstanceImpl.getConnectionManager().addConnectionHeartbeatListener(new ConnectionHeartbeatListener() {

        @Override
        public void heartbeatResumed(Connection connection) {
        }

        @Override
        public void heartbeatStopped(Connection connection) {
            heartbeatStopped.countDown();
        }
    });
    clientListenerService.registerListener(createPartitionLostListenerCodec(), new EventHandler() {

        AtomicInteger count = new AtomicInteger(0);

        @Override
        public void handle(Object event) {
        }

        @Override
        public void beforeListenerRegister() {
            if (count.incrementAndGet() == 2) {
                try {
                    blockIncoming.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public void onListenerRegister() {
            onListenerRegister.countDown();
        }
    });
    HazelcastInstance hazelcastInstance2 = hazelcastFactory.newHazelcastInstance();
    assertSizeEventually(2, clientInstanceImpl.getConnectionManager().getActiveConnections());
    blockMessagesFromInstance(hazelcastInstance2, client);
    assertOpenEventually(heartbeatStopped);
    blockIncoming.countDown();
    unblockMessagesFromInstance(hazelcastInstance2, client);
    assertOpenEventually(onListenerRegister);
}
Also used : ClientListenerService(com.hazelcast.client.spi.ClientListenerService) HazelcastInstance(com.hazelcast.core.HazelcastInstance) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Connection(com.hazelcast.nio.Connection) HazelcastClientInstanceImpl(com.hazelcast.client.impl.HazelcastClientInstanceImpl) EventHandler(com.hazelcast.client.spi.EventHandler) ConnectionHeartbeatListener(com.hazelcast.client.spi.impl.ConnectionHeartbeatListener) ClientConfig(com.hazelcast.client.config.ClientConfig) CountDownLatch(java.util.concurrent.CountDownLatch) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 60 with Connection

use of com.hazelcast.nio.Connection in project hazelcast by hazelcast.

the class AbstractListenersOnReconnectTest method validateRegistrations.

private void validateRegistrations(final int clusterSize, final String registrationId, final HazelcastClientInstanceImpl clientInstanceImpl) {
    final boolean smartRouting = clientInstanceImpl.getClientConfig().getNetworkConfig().isSmartRouting();
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            int size = smartRouting ? clusterSize : 1;
            Collection<ClientEventRegistration> registrations = getClientEventRegistrations(client, registrationId);
            assertEquals(size, registrations.size());
            if (smartRouting) {
                Collection<Member> members = clientInstanceImpl.getClientClusterService().getMemberList();
                for (ClientEventRegistration registration : registrations) {
                    Connection registeredSubscriber = registration.getSubscriber();
                    boolean contains = false;
                    for (Member member : members) {
                        contains |= registeredSubscriber.getEndPoint().equals(member.getAddress());
                    }
                    assertTrue("Registered member " + registeredSubscriber + " is not in the cluster member list " + members, contains);
                }
            } else {
                ClientEventRegistration registration = registrations.iterator().next();
                assertEquals(clientInstanceImpl.getClientClusterService().getOwnerConnectionAddress(), registration.getSubscriber().getEndPoint());
            }
        }
    });
}
Also used : ClientEventRegistration(com.hazelcast.client.spi.impl.listener.ClientEventRegistration) Connection(com.hazelcast.nio.Connection) AssertTask(com.hazelcast.test.AssertTask) Collection(java.util.Collection) Member(com.hazelcast.core.Member)

Aggregations

Connection (com.hazelcast.nio.Connection)67 Address (com.hazelcast.nio.Address)18 ClientConnection (com.hazelcast.client.connection.nio.ClientConnection)10 Test (org.junit.Test)10 QuickTest (com.hazelcast.test.annotation.QuickTest)8 IOException (java.io.IOException)7 ClientConnectionManager (com.hazelcast.client.connection.ClientConnectionManager)6 HazelcastClientInstanceImpl (com.hazelcast.client.impl.HazelcastClientInstanceImpl)6 ClientMessage (com.hazelcast.client.impl.protocol.ClientMessage)6 AssertTask (com.hazelcast.test.AssertTask)6 ClientClusterService (com.hazelcast.client.spi.ClientClusterService)4 ClientInvocation (com.hazelcast.client.spi.impl.ClientInvocation)4 Node (com.hazelcast.instance.Node)4 Packet (com.hazelcast.nio.Packet)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 EventHandler (com.hazelcast.client.spi.EventHandler)3 HazelcastInstance (com.hazelcast.core.HazelcastInstance)3 Member (com.hazelcast.core.Member)3 ILogger (com.hazelcast.logging.ILogger)3 ParallelTest (com.hazelcast.test.annotation.ParallelTest)3