May you try this:

public class PromiseExample {

    PushStreamProvider psp = null;
    SimplePushEventSource<Long> ses = null;

    public static void main(String[] args) {
        new PromiseExample().start();

    }

    private void start() {
        // Begin delivery when someone is listening
        ses.connectPromise().then(this::onConnect);

        // Create a listener which prints out even numbers
        psp.createStream(ses).
            filter(l -> l % 2L == 0).
            limit(100L).
            forEach(f -> System.out.println("Consumed event: " + f)).
            thenAccept((v)->ses.close());
    }

    private PromiseExample() {
        psp = new PushStreamProvider();
        ses = psp.createSimpleEventSource(Long.class);
    }

    private Promise<Void> onConnect(Promise<Void> promise) {
        new Thread(() -> {
            long counter = 0;
            // Keep going as long as someone is listening
            while (ses.isConnected()) {
                ses.publish(++counter);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Published: " + counter);
            }
            // Restart delivery when a new listener connects
            ses.connectPromise().then(this::onConnect);
        }).start();
        return null;
    }

}


Am 26.10.18 um 16:52 schrieb stbischof via osgi-dev:
Hello,


I tried to compile  the Pushstream Example in the Spec: 706.5.1 Optimizing Event Creation.

I got a compile exception "Local variable may not have been initialized." in line


ses.connectPromise().then(onConnect);


could somebody tell me more about how to get run this snipped

----------------------------------------------------------------------------------------------------
PushStreamProvider psp = new PushStreamProvider();

SimplePushEventSource<Long> ses = psp.createSimpleEventSource(Long.class))

Success<Void,Void> onConnect = p -> {
    new Thread(() -> {
        long counter = 0;
        // Keep going as long as someone is listening
        while (ses.isConnected()) {
          ses.publish(++counter);
          Thread.sleep(100);
          System.out.println("Published: " + counter);
        }
        // Restart delivery when a new listener connects
        ses.connectPromise().then(onConnect);
      }).start();
    return null;
  };

// Begin delivery when someone is listening
ses.connectPromise().then(onConnect);

// Create a listener which prints out even numbers
psp.createStream(ses).
  filter(l -> l % 2L == 0).
  limit(5000L).

  forEach(f -> System.out.println("Consumed event: " + f));

------------------------------------------------------------------------------------------------

regards

_______________________________________________
OSGi Developer Mail List
osgi-dev@mail.osgi.org
https://mail.osgi.org/mailman/listinfo/osgi-dev

--
Mark Hoffmann
M.A. Dipl.-Betriebswirt (FH)
CEO/CTO

Phone:   +49 3641 384 910 0
Mobile:  +49 175 701 2201
E-Mail: m.hoffm...@data-in-motion.biz
Web: www.datainmotion.de

Data In Motion Consulting GmbH
Kahlaische Straße 4
07745 Jena
Germany

Geschäftsführer
Mark Hoffmann
Jürgen Albert

Jena HRB 513025
Steuernummer 162/107/05779
USt-Id DE310002614

_______________________________________________
OSGi Developer Mail List
osgi-dev@mail.osgi.org
https://mail.osgi.org/mailman/listinfo/osgi-dev

Reply via email to