Search in sources :

Example 1 with SubscriberConf

use of org.nustaq.fastcast.config.SubscriberConf in project fast-cast by RuedigerMoeller.

the class ProgrammaticConfiguredSubscriber method main.

public static void main(String[] arg) {
    // 5 chars MAX !!
    FastCast.getFastCast().setNodeId("SUBS");
    ProgrammaticConfiguredPublisher.configureFastCast();
    FastCast.getFastCast().onTransport("default").subscribe(// listen to topic 1
    new SubscriberConf(1).receiveBufferPackets(// how many packets to buffer in case of a loss+retransmission
    20000), new ObjectSubscriber() {

        long lastMsg = System.currentTimeMillis();

        int msgReceived = 0;

        @Override
        protected void objectReceived(String sender, long sequence, Object msg) {
            msgReceived++;
            if (System.currentTimeMillis() - lastMsg > 1000) {
                System.out.println("received from " + sender + " number of msg " + msgReceived);
                System.out.println("current: " + msg);
                lastMsg = System.currentTimeMillis();
                msgReceived = 0;
            }
        }

        @Override
        public boolean dropped() {
            System.out.println("Fatal: could not keep up with send rate. exiting");
            System.exit(0);
            // do not attempt resync
            return false;
        }
    });
}
Also used : SubscriberConf(org.nustaq.fastcast.config.SubscriberConf) ObjectSubscriber(org.nustaq.fastcast.api.util.ObjectSubscriber)

Example 2 with SubscriberConf

use of org.nustaq.fastcast.config.SubscriberConf in project fast-cast by RuedigerMoeller.

the class MPSubscriber method main.

public static void main(String[] arg) {
    MultipleProtocol.initStructFactory();
    // 5 chars MAX !!
    FastCast.getFastCast().setNodeId("MSUB");
    MPPublisher.configureFastCast();
    final RateMeasure rateMeasure = new RateMeasure("receive rate");
    FastCast.getFastCast().onTransport("default").subscribe(new SubscriberConf(1).receiveBufferPackets(33_000), new FCSubscriber() {

        FSTStruct msg = FSTStructFactory.getInstance().createEmptyStructPointer(FSTStruct.class);

        int count = 0;

        @Override
        public void messageReceived(String sender, long sequence, Bytez b, long off, int len) {
            rateMeasure.count();
            msg.baseOn(b, (int) off);
            Class type = msg.getPointedClass();
            if (type == AMessage.class) {
                AMessage am = msg.cast();
            // am is valid until another pointer is used. use "am = am.detach" in order to get a non-cached pointer
            // note msg data is valid only inside this method (reused). You need to copy data in case processing should 
            // be done in a different thread (usually do basic filtering in-thread, then do processing on a dedicated thread)
            } else if (type == OtherMessage.class) {
                OtherMessage om = msg.cast();
            // ..
            } else if (type == ComposedMessage.class) {
                ComposedMessage cm = msg.cast();
                if (count++ == 500_000) {
                    System.out.println("Other" + cm);
                    count = 0;
                }
            }
        }

        @Override
        public boolean dropped() {
            System.out.println("fatal, could not keep up. exiting");
            System.exit(0);
            return false;
        }

        @Override
        public void senderTerminated(String senderNodeId) {
            System.out.println("sender died " + senderNodeId);
        }

        @Override
        public void senderBootstrapped(String receivesFrom, long seqNo) {
            System.out.println("bootstrap " + receivesFrom);
        }
    });
}
Also used : FSTStruct(org.nustaq.offheap.structs.FSTStruct) Bytez(org.nustaq.offheap.bytez.Bytez) RateMeasure(org.nustaq.fastcast.util.RateMeasure) SubscriberConf(org.nustaq.fastcast.config.SubscriberConf) FCSubscriber(org.nustaq.fastcast.api.FCSubscriber)

Example 3 with SubscriberConf

use of org.nustaq.fastcast.config.SubscriberConf in project fast-cast by RuedigerMoeller.

the class StructSubscriber method main.

public static void main(String[] arg) {
    Protocol.initStructFactory();
    // 5 chars MAX !!
    FastCast.getFastCast().setNodeId("SUB");
    StructPublisher.configureFastCast();
    final RateMeasure rateMeasure = new RateMeasure("receive rate");
    FastCast.getFastCast().onTransport("default").subscribe(new SubscriberConf(1).receiveBufferPackets(33_000), new FCSubscriber() {

        PriceUpdateStruct msg = FSTStructFactory.getInstance().createEmptyStructPointer(PriceUpdateStruct.class);

        @Override
        public void messageReceived(String sender, long sequence, Bytez b, long off, int len) {
            msg.baseOn(b, (int) off);
            rateMeasure.count();
        // instanceof'ing in case of various messages
        // Class msgStruct = msg.getPointedClass(); otherStructClass = msg.detachTo(otherStructClass);
        }

        @Override
        public boolean dropped() {
            System.out.println("fatal, could not keep up. exiting");
            System.exit(0);
            return false;
        }

        @Override
        public void senderTerminated(String senderNodeId) {
            System.out.println("sender died " + senderNodeId);
        }

        @Override
        public void senderBootstrapped(String receivesFrom, long seqNo) {
            System.out.println("bootstrap " + receivesFrom);
        }
    });
}
Also used : Bytez(org.nustaq.offheap.bytez.Bytez) RateMeasure(org.nustaq.fastcast.util.RateMeasure) SubscriberConf(org.nustaq.fastcast.config.SubscriberConf) FCSubscriber(org.nustaq.fastcast.api.FCSubscriber)

Aggregations

SubscriberConf (org.nustaq.fastcast.config.SubscriberConf)3 FCSubscriber (org.nustaq.fastcast.api.FCSubscriber)2 RateMeasure (org.nustaq.fastcast.util.RateMeasure)2 Bytez (org.nustaq.offheap.bytez.Bytez)2 ObjectSubscriber (org.nustaq.fastcast.api.util.ObjectSubscriber)1 FSTStruct (org.nustaq.offheap.structs.FSTStruct)1