Package org.cometd.bayeux.server

Examples of org.cometd.bayeux.server.LocalSession


    {
        Object service = new RemoteCallWithUncaughtExceptionService();
        boolean processed = processor.process(service);
        assertTrue(processed);

        LocalSession remote = bayeuxServer.newLocalSession("remoteCall");
        remote.handshake();
        ClientSessionChannel channel = remote.getChannel(Channel.SERVICE + RemoteCallWithUncaughtExceptionService.CHANNEL);
        final CountDownLatch latch = new CountDownLatch(1);
        channel.addListener(new ClientSessionChannel.MessageListener()
        {
            @Override
            public void onMessage(ClientSessionChannel channel, Message message)
View Full Code Here


    {
        Server serverA = startServer(0);
        final Oort oortA = startOort(serverA);
        final BayeuxServer bayeuxServerA = oortA.getBayeuxServer();

        final LocalSession serviceA = bayeuxServerA.newLocalSession("test");
        serviceA.handshake();
        final String channelName = "/test";
        final String data = "data";
        final CountDownLatch joinedLatch = new CountDownLatch(1);
        oortA.addCometListener(new Oort.CometListener.Adapter()
        {
View Full Code Here

            return false;

        List<Object> injectables = new ArrayList<>(Arrays.asList(this.injectables));
        injectables.add(0, bayeuxServer);
        boolean result = processInjectables(bean, injectables);
        LocalSession session = findOrCreateLocalSession(bean, serviceAnnotation.value());
        result |= processSession(bean, session);
        return result;
    }
View Full Code Here

            return false;

        if (!Modifier.isPublic(klass.getModifiers()))
            throw new IllegalArgumentException("Service class '" + klass.getName() + "' must be public");

        LocalSession session = findOrCreateLocalSession(bean, serviceAnnotation.value());
        boolean result = processListener(bean, session);
        result |= processSubscription(bean, session);
        result |= processRemoteCall(bean, session);
        return result;
    }
View Full Code Here

        return result;
    }

    private void destroyLocalSession(Object bean)
    {
        LocalSession session = sessions.remove(bean);
        if (session != null)
            session.disconnect();
    }
View Full Code Here

        return super.processPreDestroy(bean);
    }

    private LocalSession findOrCreateLocalSession(Object bean, String name)
    {
        LocalSession session = sessions.get(bean);
        if (session == null)
        {
            session = bayeuxServer.newLocalSession(name);
            LocalSession existing = sessions.putIfAbsent(bean, session);
            if (existing != null)
                session = existing;
            else
                session.handshake();
        }
View Full Code Here

    @Test
    public void testHighRateServerEvents() throws Exception
    {
        final String channelName = "/foo";

        LocalSession service = bayeux.newLocalSession("high_rate_test");
        service.handshake();

        final BayeuxClient client = newBayeuxClient();
        client.handshake();
        Assert.assertTrue(client.waitFor(5000, BayeuxClient.State.CONNECTED));

        final AtomicReference<CountDownLatch> latch = new AtomicReference<>(new CountDownLatch(1));
        final AtomicInteger messages = new AtomicInteger();
        client.batch(new Runnable()
        {
            public void run()
            {
                ClientSessionChannel channel = client.getChannel(channelName);
                channel.subscribe(new ClientSessionChannel.MessageListener()
                {
                    public void onMessage(ClientSessionChannel channel, Message message)
                    {
                        messages.incrementAndGet();
                        latch.get().countDown();
                    }
                });
                channel.publish(new HashMap<String, Object>());
            }
        });

        // Wait until subscription is acknowledged
        Assert.assertTrue(latch.get().await(5, TimeUnit.SECONDS));

        long begin = System.nanoTime();
        int count = 500;
        messages.set(0);
        latch.set(new CountDownLatch(count));
        for (int i = 0; i < count; ++i)
            service.getChannel(channelName).publish(new HashMap<String, Object>());
        long end = System.nanoTime();
        Assert.assertTrue(latch.get().await(count * 100, TimeUnit.MILLISECONDS));
        System.err.println("rate = " + count * 1_000_000_000L / (end - begin) + " messages/s");

        assertEquals(count, messages.get());
View Full Code Here

    }

    @Test
    public void testSessionAttributes() throws Exception
    {
        LocalSession local = _bayeux.newLocalSession("s0");
        local.handshake();
        ServerSession session = local.getServerSession();

        local.setAttribute("foo","bar");
        Assert.assertEquals("bar",local.getAttribute("foo"));
        Assert.assertEquals(null,session.getAttribute("foo"));

        session.setAttribute("bar","foo");
        Assert.assertEquals(null,local.getAttribute("bar"));
        Assert.assertEquals("foo",session.getAttribute("bar"));

        Assert.assertTrue(local.getAttributeNames().contains("foo"));
        Assert.assertFalse(local.getAttributeNames().contains("bar"));
        Assert.assertFalse(session.getAttributeNames().contains("foo"));
        Assert.assertTrue(session.getAttributeNames().contains("bar"));

        Assert.assertEquals("bar",local.removeAttribute("foo"));
        Assert.assertEquals(null,local.removeAttribute("foo"));
        Assert.assertEquals("foo",session.removeAttribute("bar"));
        Assert.assertEquals(null,local.removeAttribute("bar"));
    }
View Full Code Here

    }

    @Test
    public void testLocalSessions() throws Exception
    {
        LocalSession session0 = _bayeux.newLocalSession("s0");
        Assert.assertEquals("L:s0_", session0.toString());
        session0.handshake();
        Assert.assertNotEquals("L:s0_", session0.toString());
        Assert.assertTrue(session0.toString().startsWith("L:s0_"));

        final LocalSession session1 = _bayeux.newLocalSession("s1");
        session1.handshake();
        final LocalSession session2 = _bayeux.newLocalSession("s2");
        session2.handshake();

        final Queue<String> events = new ConcurrentLinkedQueue<>();

        ClientSessionChannel.MessageListener listener = new ClientSessionChannel.MessageListener()
        {
            public void onMessage(ClientSessionChannel channel, Message message)
            {
                events.add(channel.getSession().getId());
                events.add(message.getData().toString());
            }
        };

        session0.getChannel("/foo/bar").subscribe(listener);
        session0.getChannel("/foo/bar").subscribe(listener);
        session1.getChannel("/foo/bar").subscribe(listener);
        session2.getChannel("/foo/bar").subscribe(listener);

        Assert.assertEquals(3,_bayeux.getChannel("/foo/bar").getSubscribers().size());

        session0.getChannel("/foo/bar").unsubscribe(listener);
        Assert.assertEquals(3,_bayeux.getChannel("/foo/bar").getSubscribers().size());
        session0.getChannel("/foo/bar").unsubscribe(listener);
        Assert.assertEquals(2,_bayeux.getChannel("/foo/bar").getSubscribers().size());

        ClientSessionChannel foobar0=session0.getChannel("/foo/bar");
        foobar0.subscribe(listener);
        foobar0.subscribe(listener);

        ClientSessionChannel foostar0=session0.getChannel("/foo/*");
        foostar0.subscribe(listener);

        Assert.assertEquals(3,_bayeux.getChannel("/foo/bar").getSubscribers().size());
        Assert.assertEquals(session0,foobar0.getSession());
        Assert.assertEquals("/foo/bar",foobar0.getId());
        Assert.assertEquals(false,foobar0.isDeepWild());
        Assert.assertEquals(false,foobar0.isWild());
        Assert.assertEquals(false,foobar0.isMeta());
        Assert.assertEquals(false,foobar0.isService());

        foobar0.publish("hello");

        Assert.assertEquals(session0.getId(),events.poll());
        Assert.assertEquals("hello",events.poll());
        Assert.assertEquals(session0.getId(),events.poll());
        Assert.assertEquals("hello",events.poll());
        Assert.assertEquals(session0.getId(),events.poll());
        Assert.assertEquals("hello",events.poll());
        Assert.assertEquals(session1.getId(),events.poll());
        Assert.assertEquals("hello",events.poll());
        Assert.assertEquals(session2.getId(),events.poll());
        Assert.assertEquals("hello",events.poll());
        foostar0.unsubscribe(listener);

        session1.batch(new Runnable()
        {
            public void run()
            {
                ClientSessionChannel foobar1=session1.getChannel("/foo/bar");
                foobar1.publish("part1");
                Assert.assertEquals(null,events.poll());
                foobar1.publish("part2");
            }
        });

        Assert.assertEquals(session1.getId(),events.poll());
        Assert.assertEquals("part1",events.poll());
        Assert.assertEquals(session2.getId(),events.poll());
        Assert.assertEquals("part1",events.poll());
        Assert.assertEquals(session0.getId(),events.poll());
        Assert.assertEquals("part1",events.poll());
        Assert.assertEquals(session0.getId(),events.poll());
        Assert.assertEquals("part1",events.poll());
        Assert.assertEquals(session1.getId(),events.poll());
        Assert.assertEquals("part2",events.poll());
        Assert.assertEquals(session2.getId(),events.poll());
        Assert.assertEquals("part2",events.poll());
        Assert.assertEquals(session0.getId(),events.poll());
        Assert.assertEquals("part2",events.poll());
        Assert.assertEquals(session0.getId(),events.poll());
        Assert.assertEquals("part2",events.poll());

        foobar0.unsubscribe();
        Assert.assertEquals(2,_bayeux.getChannel("/foo/bar").getSubscribers().size());

        Assert.assertTrue(session0.isConnected());
        Assert.assertTrue(session1.isConnected());
        Assert.assertTrue(session2.isConnected());
        ServerSession ss0=session0.getServerSession();
        ServerSession ss1=session1.getServerSession();
        ServerSession ss2=session2.getServerSession();
        Assert.assertTrue(ss0.isConnected());
        Assert.assertTrue(ss1.isConnected());
        Assert.assertTrue(ss2.isConnected());

        session0.disconnect();
        Assert.assertFalse(session0.isConnected());
        Assert.assertFalse(ss0.isConnected());

        session1.getServerSession().disconnect();
        Assert.assertFalse(session1.isConnected());
        Assert.assertFalse(ss1.isConnected());

        session2.getServerSession().disconnect();
        Assert.assertFalse(session2.isConnected());
        Assert.assertFalse(ss2.isConnected());
    }
View Full Code Here

                    message.setData("two");
                return !"ignoreRcv".equals(message.getData());
            }
        });

        final LocalSession session0 = _bayeux.newLocalSession("s0");
        session0.handshake();
        //final LocalSession session1 = _bayeux.newLocalSession("s1");
        //session1.handshake();

        session0.addExtension(new ClientSession.Extension.Adapter()
        {
            @Override
            public boolean send(ClientSession session, org.cometd.bayeux.Message.Mutable message)
            {
                if ("zero".equals(message.getData()))
                    message.setData("one");
                return true;
            }

            @Override
            public boolean rcv(ClientSession session, org.cometd.bayeux.Message.Mutable message)
            {
                if ("five".equals(message.getData()))
                    message.setData("six");
                return true;
            }
        });

        session0.getServerSession().addExtension(new ServerSession.Extension.Adapter()
        {
            @Override
            public boolean rcv(ServerSession from, Mutable message)
            {
                if ("two".equals(message.getData()))
                    message.setData("three");
                return true;
            }

            @Override
            public ServerMessage send(ServerSession session, ServerMessage message)
            {
                if (message.isMeta())
                    new Throwable().printStackTrace();
                if ("four".equals(message.getData()))
                {
                    ServerMessage.Mutable cloned=_bayeux.newMessage(message);
                    cloned.setData("five");
                    return cloned;
                }
                return message;
            }
        });

        ClientSessionChannel.MessageListener listener = new ClientSessionChannel.MessageListener()
        {
            public void onMessage(ClientSessionChannel channel, Message message)
            {
                events.add(channel.getSession().getId());
                events.add(message.getData().toString());
            }
        };

        session0.getChannel("/foo/bar").subscribe(listener);
        // session1.getChannel("/foo/bar").subscribe(listener);

        session0.getChannel("/foo/bar").publish("zero");
        session0.getChannel("/foo/bar").publish("ignoreSend");
        session0.getChannel("/foo/bar").publish("ignoreRcv");

        Thread.sleep(100);

        Assert.assertEquals(session0.getId(),events.poll());
        Assert.assertEquals("six",events.poll());

        /*
        Assert.assertEquals(session1.getId(),events.poll());
        Assert.assertEquals("four",events.poll());
View Full Code Here

TOP

Related Classes of org.cometd.bayeux.server.LocalSession

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.