Beispiel #1
0
  public static void main(String[] args) {
    try {
      DefaultWampChannelFactory factory = new DefaultWampChannelFactory();

      WampChannel channel = factory.createJsonChannel(new URI("ws://127.0.0.1:8080/ws"), "realm1");

      CompletionStage open = channel.open();

      open.toCompletableFuture().get(5000, TimeUnit.MILLISECONDS);

      WampRealmServiceProvider services = channel.getRealmProxy().getServices();

      Subject<Integer, Integer> subject = services.getSubject(Integer.class, "com.myapp.topic1");

      Subscription disposable =
          subject.subscribe(
              new Action1<Integer>() {
                @Override
                public void call(Integer counter) {
                  System.out.println("Got " + counter);
                }
              });

      System.in.read();

      disposable.unsubscribe();

      System.in.read();

    } catch (Exception ex) {
      // Catch everything! :(
      System.out.println(ex);
    }
  }
  @Test
  public void testNext() {
    Subject<String, String> obs = PublishSubject.create();
    Iterator<String> it = next(obs).iterator();
    fireOnNextInNewThread(obs, "one");
    assertTrue(it.hasNext());
    assertEquals("one", it.next());

    fireOnNextInNewThread(obs, "two");
    assertTrue(it.hasNext());
    assertEquals("two", it.next());

    obs.onCompleted();
    assertFalse(it.hasNext());
    try {
      it.next();
      fail("At the end of an iterator should throw a NoSuchElementException");
    } catch (NoSuchElementException e) {
    }

    // If the observable is completed, hasNext always returns false and next always throw a
    // NoSuchElementException.
    assertFalse(it.hasNext());
    try {
      it.next();
      fail("At the end of an iterator should throw a NoSuchElementException");
    } catch (NoSuchElementException e) {
    }
  }
 /**
  * 发送事件
  *
  * @param tag 标签
  * @param content 发送的内容
  */
 @SuppressWarnings("unchecked")
 public void post(@NonNull Object tag, @NonNull Object content) {
   final List<Subject> subjectList = subjectMapper.get(tag);
   if (null != subjectList && !subjectList.isEmpty()) {
     for (Subject subject : subjectList) {
       subject.onNext(content);
     }
   }
 }
 @Test
 public void testUDPconnection() throws Exception {
   Subject<byte[], byte[]> sub = PublishSubject.create();
   SimpleChan simpleadapter = new SimpleChan(sub);
   TestAwaitableObserver<byte[]> monitor = new TestAwaitableObserver<>();
   sub.subscribe(monitor);
   int port = rand.nextInt(100) + 8000;
   ServerFrontEnd udpSrv = ServerFrontEnd.udp(simpleadapter, port);
   udpSrv.startAsync().awaitRunning();
   sendMSG(MSG.getBytes(), port);
   monitor.awaitOnNext(1, TIMEOUT);
   assertThat("msg was received", monitor.getOnNextEvents().get(0), equalTo(MSG.getBytes()));
 }
  @Test
  public void testOnError() throws Throwable {
    Subject<String, String> obs = PublishSubject.create();
    Iterator<String> it = next(obs).iterator();

    obs.onError(new TestException());
    try {
      it.hasNext();
      fail("Expected an TestException");
    } catch (TestException e) {
      // successful
    }

    assertErrorAfterObservableFail(it);
  }
 @Test
 public void testUDPLargePacket() throws Exception {
   Subject<byte[], byte[]> sub = PublishSubject.create();
   SimpleChan simpleadapter = new SimpleChan(sub);
   TestAwaitableObserver<byte[]> monitor = new TestAwaitableObserver<>();
   sub.subscribe(monitor);
   int port = rand.nextInt(100) + 8000;
   ServerFrontEnd udpSrv = ServerFrontEnd.udp(simpleadapter, port, 65536);
   byte[] msg = new byte[65535 - 28]; // max payload size
   rand.nextBytes(msg);
   udpSrv.startAsync().awaitRunning();
   sendMSG(msg, port);
   monitor.awaitOnNext(1, TIMEOUT);
   assertThat("msg was received", monitor.getOnNextEvents().get(0), equalTo(msg));
 }
  @Test
  public void testNextWithOnlyUsingNextMethod() {
    Subject<String, String> obs = PublishSubject.create();
    Iterator<String> it = next(obs).iterator();
    fireOnNextInNewThread(obs, "one");
    assertEquals("one", it.next());

    fireOnNextInNewThread(obs, "two");
    assertEquals("two", it.next());

    obs.onCompleted();
    try {
      it.next();
      fail("At the end of an iterator should throw a NoSuchElementException");
    } catch (NoSuchElementException e) {
    }
  }
  HystrixCommandCompletionStream(final HystrixCommandKey commandKey) {
    this.commandKey = commandKey;

    this.writeOnlySubject =
        new SerializedSubject<HystrixCommandCompletion, HystrixCommandCompletion>(
            PublishSubject.<HystrixCommandCompletion>create());
    this.readOnlyStream = writeOnlySubject.share();
  }
  /**
   * Creates a subject whose client observers will observe events propagated through the given
   * wrapped subject.
   *
   * @param <T> the element type
   * @param subject the subject to wrap
   * @param scheduler the target scheduler
   * @return the created subject
   */
  public static <T> Subject<T, T> createScheduledSubject(
      Subject<T, T> subject, Scheduler scheduler) {
    final Observable<T> observedOn = subject.observeOn(scheduler);
    SubjectWrapper<T> s =
        new SubjectWrapper<T>(
            new OnSubscribe<T>() {

              @Override
              public void call(Subscriber<? super T> o) {
                subscriberOf(observedOn).call(o);
              }
            },
            subject);
    return s;
  }
 public void write(HystrixCommandCompletion event) {
   writeOnlySubject.onNext(event);
 }
 @Override
 public void onError(Throwable e) {
   publish.onError(e);
 }
 @Override
 public void onError(Throwable e) {
   subject.onError(e);
 }
 @Override
 public void onNext(T args) {
   subject.onNext(args);
 }
  public void init() {
    log.info("UpdateFX current version " + Version.PATCH_VERSION);

    // process.timeout() will cause an error state back but we don't want to break startup in case
    // of an timeout
    timeoutTimer =
        Utilities.setTimeout(
            10000,
            () -> {
              log.error("Timeout reached for UpdateFX");
              process.onCompleted();
            });
    String userAgent = environment.getProperty(BitsquareEnvironment.APP_NAME_KEY) + Version.VERSION;

    // Check if there is a new minor version release out. The release_url should be empty if no
    // release is available, otherwise the download url.
    try {
      releaseUrl = Utilities.readTextFileFromServer(UPDATES_BASE_URL + "/release_url", userAgent);
      if (releaseUrl != null && releaseUrl.length() > 0) {
        log.info("New release available at: " + releaseUrl);
        state.set(State.NEW_RELEASE);
        timeoutTimer.cancel();
        return;
      } else {
        // All ok. Empty file if we have no new release.
      }
    } catch (IOException e) {
      // ignore. File might be missing
    }

    Updater updater =
        new Updater(
            UPDATES_BASE_URL,
            userAgent,
            Version.PATCH_VERSION,
            Paths.get(environment.getProperty(BitsquareEnvironment.APP_DATA_DIR_KEY)),
            ROOT_CLASS_PATH,
            UPDATE_SIGNING_KEYS,
            UPDATE_SIGNING_THRESHOLD) {
          @Override
          protected void updateProgress(long workDone, long max) {
            // log.trace("updateProgress " + workDone + "/" + max);
            super.updateProgress(workDone, max);
          }
        };

    /* updater.progressProperty().addListener((observableValue, oldValue, newValue) -> {
        log.trace("progressProperty newValue = " + newValue);
    });*/

    log.info("Checking for updates!");
    updater.setOnSucceeded(
        event -> {
          try {
            UpdateSummary summary = updater.get();
            log.info("summary " + summary.toString());
            if (summary.descriptions != null && summary.descriptions.size() > 0) {
              log.info("One liner: {}", summary.descriptions.get(0).getOneLiner());
              log.info("{}", summary.descriptions.get(0).getDescription());
            }
            if (summary.highestVersion > Version.PATCH_VERSION) {
              log.info("UPDATE_AVAILABLE");
              state.set(State.UPDATE_AVAILABLE);
              // We stop the timeout and treat it not completed.
              // The user should click the restart button manually if there are updates available.
              timeoutTimer.cancel();
            } else if (summary.highestVersion == Version.PATCH_VERSION) {
              log.info("UP_TO_DATE");
              state.set(State.UP_TO_DATE);
              timeoutTimer.cancel();
              process.onCompleted();
            }
          } catch (Throwable e) {
            log.error("Exception at processing UpdateSummary: " + e.getMessage());

            // we treat errors as update not as critical errors to prevent startup,
            // so we use state.onCompleted() instead of state.onError()
            state.set(State.FAILURE);
            timeoutTimer.cancel();
            process.onCompleted();
          }
        });
    updater.setOnFailed(
        event -> {
          log.error("Update failed: " + updater.getException());
          updater.getException().printStackTrace();

          // we treat errors as update not as critical errors to prevent startup,
          // so we use state.onCompleted() instead of state.onError()
          state.set(State.FAILURE);
          timeoutTimer.cancel();
          process.onCompleted();
        });

    Thread thread = new Thread(updater, "Online update check");
    thread.setDaemon(true);
    thread.start();
  }
 public Observable<State> getProcess() {
   return process.asObservable();
 }
 @Override
 public Subscription subscribe(Observer<? super V> observer) {
   return publish.subscribe(observer);
 }
 @Override
 public void onNext(V args) {
   publish.onNext(args);
 }
 @Override
 public void onCompleted() {
   subject.onCompleted();
 }
 @Override
 public void onCompleted() {
   publish.onCompleted();
 }
Beispiel #20
0
 public void send(Object o) {
   _bus.onNext(o);
 }
Beispiel #21
0
 public boolean hasObservers() {
   return _bus.hasObservers();
 }
Beispiel #22
0
 public void send(Object o) {
   LLog.d("RxBus send: " + o.toString());
   _bus.onNext(o);
 }