Search in sources :

Example 71 with ClusterGroup

use of org.apache.ignite.cluster.ClusterGroup in project ignite by apache.

the class IgniteMessagingConfigVariationFullApiTest method clientServerOrderedMessage.

/**
 * @throws Exception If fail.
 */
private void clientServerOrderedMessage() throws Exception {
    Ignite ignite = grid(CLIENT_NODE_IDX);
    ClusterGroup grp = ignite.cluster().forServers();
    assert !grp.nodes().isEmpty();
    registerListenerAndSendOrderedMessages(ignite, grp);
}
Also used : ClusterGroup(org.apache.ignite.cluster.ClusterGroup) Ignite(org.apache.ignite.Ignite)

Example 72 with ClusterGroup

use of org.apache.ignite.cluster.ClusterGroup in project ignite by apache.

the class GridMessagingSelfTest method testStopLocalListen.

/**
 * @throws Exception If error occurs.
 */
@SuppressWarnings("TooBroadScope")
@Test
public void testStopLocalListen() throws Exception {
    final AtomicInteger msgCnt1 = new AtomicInteger();
    final AtomicInteger msgCnt2 = new AtomicInteger();
    final AtomicInteger msgCnt3 = new AtomicInteger();
    P2<UUID, Object> lsnr1 = new P2<UUID, Object>() {

        @Override
        public boolean apply(UUID nodeId, Object msg) {
            log.info("Listener1 received new message [msg=" + msg + ", senderNodeId=" + nodeId + ']');
            msgCnt1.incrementAndGet();
            return true;
        }
    };
    P2<UUID, Object> lsnr2 = new P2<UUID, Object>() {

        @Override
        public boolean apply(UUID nodeId, Object msg) {
            log.info("Listener2 received new message [msg=" + msg + ", senderNodeId=" + nodeId + ']');
            msgCnt2.incrementAndGet();
            return true;
        }
    };
    P2<UUID, Object> lsnr3 = new P2<UUID, Object>() {

        @Override
        public boolean apply(UUID nodeId, Object msg) {
            log.info("Listener3 received new message [msg=" + msg + ", senderNodeId=" + nodeId + ']');
            msgCnt3.incrementAndGet();
            return true;
        }
    };
    final String topic1 = null;
    final String topic2 = "top1";
    final String topic3 = "top3";
    ignite1.message().localListen(topic1, lsnr1);
    ignite1.message().localListen(topic2, lsnr2);
    ignite1.message().localListen(topic3, lsnr3);
    ClusterGroup rNode1 = ignite2.cluster().forRemotes();
    message(rNode1).send(topic1, "msg1-1");
    message(rNode1).send(topic2, "msg1-2");
    message(rNode1).send(topic3, "msg1-3");
    GridTestUtils.waitForCondition(new PA() {

        @Override
        public boolean apply() {
            return msgCnt1.get() > 0 && msgCnt2.get() > 0 && msgCnt3.get() > 0;
        }
    }, 5000);
    assertEquals(1, msgCnt1.get());
    assertEquals(1, msgCnt2.get());
    assertEquals(1, msgCnt3.get());
    ignite1.message().stopLocalListen(topic2, lsnr2);
    message(rNode1).send(topic1, "msg2-1");
    message(rNode1).send(topic2, "msg2-2");
    message(rNode1).send(topic3, "msg2-3");
    GridTestUtils.waitForCondition(new PA() {

        @Override
        public boolean apply() {
            return msgCnt1.get() > 1 && msgCnt3.get() > 1;
        }
    }, 5000);
    assertEquals(2, msgCnt1.get());
    assertEquals(1, msgCnt2.get());
    assertEquals(2, msgCnt3.get());
    // Try to use wrong topic for lsnr1 removing.
    ignite1.message().stopLocalListen(topic2, lsnr1);
    message(rNode1).send(topic1, "msg3-1");
    message(rNode1).send(topic2, "msg3-2");
    message(rNode1).send(topic3, "msg3-3");
    GridTestUtils.waitForCondition(new PA() {

        @Override
        public boolean apply() {
            return msgCnt1.get() > 2 && msgCnt3.get() > 2;
        }
    }, 5000);
    assertEquals(3, msgCnt1.get());
    assertEquals(1, msgCnt2.get());
    assertEquals(3, msgCnt3.get());
    ignite1.message().stopLocalListen(topic1, lsnr1);
    ignite1.message().stopLocalListen(topic3, lsnr3);
    message(rNode1).send(topic1, "msg4-1");
    message(rNode1).send(topic2, "msg4-2");
    message(rNode1).send(topic3, "msg4-3");
    U.sleep(1000);
    assertEquals(3, msgCnt1.get());
    assertEquals(1, msgCnt2.get());
    assertEquals(3, msgCnt3.get());
}
Also used : PA(org.apache.ignite.internal.util.typedef.PA) P2(org.apache.ignite.internal.util.typedef.P2) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClusterGroup(org.apache.ignite.cluster.ClusterGroup) UUID(java.util.UUID) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 73 with ClusterGroup

use of org.apache.ignite.cluster.ClusterGroup in project ignite by apache.

the class GridMessagingSelfTest method testSendReceiveMessageWithEnumTopic.

/**
 * Tests simple message sending-receiving with enumerated topic.
 *
 * @throws Exception If error occurs.
 */
@Test
public void testSendReceiveMessageWithEnumTopic() throws Exception {
    final Collection<Object> rcvMsgs = new GridConcurrentHashSet<>();
    // to make it modifiable
    final AtomicBoolean error = new AtomicBoolean(false);
    final CountDownLatch rcvLatch = new CountDownLatch(3);
    ignite1.message().localListen(TestTopic.TOPIC_1, new P2<UUID, Object>() {

        @Override
        public boolean apply(UUID nodeId, Object msg) {
            try {
                log.info("Received new message [msg=" + msg + ", senderNodeId=" + nodeId + ", topic=" + TestTopic.TOPIC_1 + ']');
                if (!nodeId.equals(ignite1.cluster().localNode().id())) {
                    log.error("Unexpected sender node: " + nodeId);
                    error.set(true);
                    return false;
                }
                if (!MSG_1.equals(msg)) {
                    log.error("Unexpected message " + msg + " for topic: " + TestTopic.TOPIC_1);
                    error.set(true);
                    return false;
                }
                rcvMsgs.add(msg);
                return true;
            } finally {
                rcvLatch.countDown();
            }
        }
    });
    ignite1.message().localListen(TestTopic.TOPIC_2, new P2<UUID, Object>() {

        @Override
        public boolean apply(UUID nodeId, Object msg) {
            try {
                log.info("Received new message [msg=" + msg + ", senderNodeId=" + nodeId + ", topic=" + TestTopic.TOPIC_2 + ']');
                if (!nodeId.equals(ignite1.cluster().localNode().id())) {
                    log.error("Unexpected sender node: " + nodeId);
                    error.set(true);
                    return false;
                }
                if (!MSG_2.equals(msg)) {
                    log.error("Unexpected message " + msg + " for topic: " + TestTopic.TOPIC_2);
                    error.set(true);
                    return false;
                }
                rcvMsgs.add(msg);
                return true;
            } finally {
                rcvLatch.countDown();
            }
        }
    });
    ignite1.message().localListen(null, new P2<UUID, Object>() {

        @Override
        public boolean apply(UUID nodeId, Object msg) {
            try {
                log.info("Received new message [msg=" + msg + ", senderNodeId=" + nodeId + ", topic=default]");
                if (!nodeId.equals(ignite1.cluster().localNode().id())) {
                    log.error("Unexpected sender node: " + nodeId);
                    error.set(true);
                    return false;
                }
                if (!MSG_3.equals(msg)) {
                    log.error("Unexpected message " + msg + " for topic: default");
                    error.set(true);
                    return false;
                }
                rcvMsgs.add(msg);
                return true;
            } finally {
                rcvLatch.countDown();
            }
        }
    });
    ClusterGroup rNode1 = ignite1.cluster().forLocal();
    message(rNode1).send(TestTopic.TOPIC_1, MSG_1);
    message(rNode1).send(TestTopic.TOPIC_2, MSG_2);
    message(rNode1).send(null, MSG_3);
    assertTrue(rcvLatch.await(3, TimeUnit.SECONDS));
    assertFalse(error.get());
    assertTrue(rcvMsgs.contains(MSG_1));
    assertTrue(rcvMsgs.contains(MSG_2));
    assertTrue(rcvMsgs.contains(MSG_3));
}
Also used : GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClusterGroup(org.apache.ignite.cluster.ClusterGroup) CountDownLatch(java.util.concurrent.CountDownLatch) UUID(java.util.UUID) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 74 with ClusterGroup

use of org.apache.ignite.cluster.ClusterGroup in project ignite by apache.

the class GridMessagingSelfTest method testSendReceiveMessage.

/**
 * Tests simple message sending-receiving.
 *
 * @throws Exception If error occurs.
 */
@Test
public void testSendReceiveMessage() throws Exception {
    final Collection<Object> rcvMsgs = new GridConcurrentHashSet<>();
    // to make it modifiable
    final AtomicBoolean error = new AtomicBoolean(false);
    final CountDownLatch rcvLatch = new CountDownLatch(3);
    ignite1.message().localListen(null, new P2<UUID, Object>() {

        @Override
        public boolean apply(UUID nodeId, Object msg) {
            try {
                log.info("Received new message [msg=" + msg + ", senderNodeId=" + nodeId + ']');
                if (!nodeId.equals(ignite2.cluster().localNode().id())) {
                    log.error("Unexpected sender node: " + nodeId);
                    error.set(true);
                    return false;
                }
                rcvMsgs.add(msg);
                return true;
            } finally {
                rcvLatch.countDown();
            }
        }
    });
    ClusterGroup rNode1 = ignite2.cluster().forRemotes();
    message(rNode1).send(null, MSG_1);
    message(rNode1).send(null, MSG_2);
    message(rNode1).send(null, MSG_3);
    assertTrue(rcvLatch.await(3, TimeUnit.SECONDS));
    assertFalse(error.get());
    assertTrue(rcvMsgs.contains(MSG_1));
    assertTrue(rcvMsgs.contains(MSG_2));
    assertTrue(rcvMsgs.contains(MSG_3));
}
Also used : GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClusterGroup(org.apache.ignite.cluster.ClusterGroup) CountDownLatch(java.util.concurrent.CountDownLatch) UUID(java.util.UUID) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 75 with ClusterGroup

use of org.apache.ignite.cluster.ClusterGroup in project ignite by apache.

the class GridMessagingSelfTest method remoteListenForOldest.

/**
 * @param expOldestIgnite Expected oldest ignite.
 * @throws InterruptedException If failed.
 */
private void remoteListenForOldest(Ignite expOldestIgnite) throws InterruptedException {
    ClusterGroup grp = ignite1.cluster().forOldest();
    assertEquals(1, grp.nodes().size());
    assertEquals(expOldestIgnite.cluster().localNode().id(), grp.node().id());
    ignite1.message(grp).remoteListen(null, new P2<UUID, Object>() {

        @Override
        public boolean apply(UUID nodeId, Object msg) {
            System.out.println("Received new message [msg=" + msg + ", senderNodeId=" + nodeId + ']');
            MSG_CNT.incrementAndGet();
            return true;
        }
    });
    ignite1.message().send(null, MSG_1);
    Thread.sleep(3000);
    assertEquals(1, MSG_CNT.get());
}
Also used : ClusterGroup(org.apache.ignite.cluster.ClusterGroup) UUID(java.util.UUID)

Aggregations

ClusterGroup (org.apache.ignite.cluster.ClusterGroup)99 Ignite (org.apache.ignite.Ignite)51 Test (org.junit.Test)49 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)36 UUID (java.util.UUID)26 CountDownLatch (java.util.concurrent.CountDownLatch)18 ClusterNode (org.apache.ignite.cluster.ClusterNode)16 GridCommonTest (org.apache.ignite.testframework.junits.common.GridCommonTest)14 IgniteException (org.apache.ignite.IgniteException)12 ArrayList (java.util.ArrayList)9 IgniteCompute (org.apache.ignite.IgniteCompute)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 IgniteCluster (org.apache.ignite.IgniteCluster)6 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)6 GridConcurrentHashSet (org.apache.ignite.internal.util.GridConcurrentHashSet)5 IgniteFuture (org.apache.ignite.lang.IgniteFuture)5 Map (java.util.Map)4 IgniteEx (org.apache.ignite.internal.IgniteEx)4 IgniteClusterEx (org.apache.ignite.internal.cluster.IgniteClusterEx)4 GridAbsPredicate (org.apache.ignite.internal.util.lang.GridAbsPredicate)4