Esempio n. 1
0
  private void dispatchMessages() throws IOException {
    Iterator<SelectionKey> it = selector.selectedKeys().iterator();
    while (it.hasNext()) {
      SelectionKey key = it.next();
      EventSource source = (EventSource) key.attachment();

      if (key.isValid()) {
        try {
          if (key.isAcceptable()) {
            EventSource newSource = source.accept();
            if (newSource != null) newSources.add(newSource);
          }

          if (key.isReadable()) {
            source.read();
          }

          if (key.isWritable()) {
            source.write();
          }
        } catch (CancelledKeyException e) {
        }
      }

      it.remove();
    }
    for (int i = 0; i < newSources.size(); i++) register(newSources.get(i));

    if (!newSources.isEmpty()) newSources.clear();
  }
  public GradleSourceForBinaryQuery(JavaExtension javaExt) {
    if (javaExt == null) throw new NullPointerException("javaExt");
    this.javaExt = javaExt;

    EventSource eventSource = new EventSource();
    this.changes = new ChangeSupport(eventSource);
    eventSource.init(this.changes);
  }
Esempio n. 3
0
  public static void main(String[] args) {
    final EventSource es = new EventSource();
    final ResponseHandler rh = new ResponseHandler();

    es.addObserver(rh);

    Thread th = new Thread(es);
    th.start();
  }
  @Override
  public Registration addHandler(final EventHandler<? super TargetEventT> handler) {
    if (myHandlers.isEmpty()) {
      mySourceHandler =
          mySourceEventSource.addHandler(
              new EventHandler<SourceEventT>() {
                @Override
                public void onEvent(SourceEventT item) {
                  final TargetEventT targetEvent = myFunction.apply(item);
                  myHandlers.fire(
                      new ListenerCaller<EventHandler<? super TargetEventT>>() {
                        @Override
                        public void call(EventHandler<? super TargetEventT> item) {
                          item.onEvent(targetEvent);
                        }
                      });
                }
              });
    }
    final Registration reg = myHandlers.add(handler);

    return new Registration() {
      @Override
      protected void doRemove() {
        reg.remove();
        if (myHandlers.isEmpty()) {
          mySourceHandler.remove();
        }
      }
    };
  }
Esempio n. 5
0
 public ThisEscape(EventSource source) {
   source.registerListener(
       new EventListener() {
         public void onEvent(Event e) {
           doSomething(e);
         }
       });
 }
  @Test
  public void test() {
    EventSource<Integer> source = new EventSource<>();
    SuspendableEventStream<Integer> suspendable = source.pausable();
    List<Integer> emitted = new ArrayList<>();
    suspendable.subscribe(emitted::add);

    source.push(1);
    suspendable.suspendWhile(
        () -> {
          source.push(2);
          source.push(3);
        });
    source.push(4);

    assertEquals(Arrays.asList(1, 2, 3, 4), emitted);
  }
 public void contextDestroyed() {
   if (hornetqServer != null) {
     eventSource.shutDown();
     try {
       hornetqServer.stop();
       log.info("Hornetq server stopped.");
     } catch (Exception e) {
       log.error("Error stopping hornetq server", e);
     }
   }
 }
Esempio n. 8
0
 @Override
 void writeBody(final BufferedSource in, final BufferedSink out) throws IOException {
   out.writeUtf8("retry: " + mRetrySecs + "\n").flush();
   mEventSource.connect(new Body(in, out));
 }
 /**
  * The source of the event, such as an AWS service, that triggers AWS Config to evaluate your AWS
  * resources.
  *
  * <p>Returns a reference to this object so that method calls can be chained together.
  *
  * <p><b>Constraints:</b><br>
  * <b>Allowed Values: </b>aws.config
  *
  * @param eventSource The source of the event, such as an AWS service, that triggers AWS Config to
  *     evaluate your AWS resources.
  * @return A reference to this updated object so that method calls can be chained together.
  * @see EventSource
  */
 public SourceDetail withEventSource(EventSource eventSource) {
   this.eventSource = eventSource.toString();
   return this;
 }
 /**
  * The source of the event, such as an AWS service, that triggers AWS Config to evaluate your AWS
  * resources.
  *
  * <p><b>Constraints:</b><br>
  * <b>Allowed Values: </b>aws.config
  *
  * @param eventSource The source of the event, such as an AWS service, that triggers AWS Config to
  *     evaluate your AWS resources.
  * @see EventSource
  */
 public void setEventSource(EventSource eventSource) {
   this.eventSource = eventSource.toString();
 }
Esempio n. 11
0
 public void register(EventSource source) throws IOException {
   SelectionKey result = source.register(this);
   result.attach(source);
   sources.add(source);
 }
Esempio n. 12
0
  @Test
  public void countDownTest() {
    EventSource<Void> src1 = new EventSource<Void>();
    EventSource<Void> src2 = new EventSource<Void>();
    EventSource<Void> reset = new EventSource<Void>();

    BiFunction<Integer, Void, Tuple2<Integer, Optional<String>>> countdown =
        (s, i) -> s == 1 ? t(3, Optional.of("COUNTDOWN REACHED")) : t(s - 1, Optional.empty());

    EventStream<String> countdowns =
        StateMachine.init(3)
            .on(src1)
            .transmit(countdown)
            .on(src2)
            .transmit(countdown)
            .on(reset)
            .transition((s, i) -> 3)
            .toEventStream();

    Counter counter = new Counter();
    Subscription sub = countdowns.hook(x -> counter.inc()).pin();

    src1.push(null);
    src2.push(null);
    assertEquals(0, counter.get());

    src1.push(null);
    assertEquals(1, counter.getAndReset());

    src2.push(null);
    src2.push(null);
    reset.push(null);
    assertEquals(0, counter.get());
    src2.push(null);
    assertEquals(0, counter.get());
    src1.push(null);
    assertEquals(0, counter.get());
    src2.push(null);
    assertEquals(1, counter.getAndReset());

    sub.unsubscribe();
    src1.push(null);
    src1.push(null);
    src1.push(null);
    src1.push(null);
    src1.push(null);
    src1.push(null);
    assertEquals(0, counter.get());
  }
  public void contextInitialized(Injector injector) {
    org.candlepin.common.config.Configuration candlepinConfig =
        injector.getInstance(org.candlepin.common.config.Configuration.class);

    if (hornetqServer == null) {
      Configuration config = new ConfigurationImpl();

      HashSet<TransportConfiguration> transports = new HashSet<TransportConfiguration>();
      transports.add(new TransportConfiguration(InVMAcceptorFactory.class.getName()));
      config.setAcceptorConfigurations(transports);

      // alter the default pass to silence log output
      config.setClusterUser(null);
      config.setClusterPassword(null);

      // in vm, who needs security?
      config.setSecurityEnabled(false);

      config.setJournalType(JournalType.NIO);

      config.setCreateBindingsDir(true);
      config.setCreateJournalDir(true);

      String baseDir = candlepinConfig.getString(ConfigProperties.HORNETQ_BASE_DIR);

      config.setBindingsDirectory(new File(baseDir, "bindings").toString());
      config.setJournalDirectory(new File(baseDir, "journal").toString());
      config.setLargeMessagesDirectory(new File(baseDir, "largemsgs").toString());
      config.setPagingDirectory(new File(baseDir, "paging").toString());

      Map<String, AddressSettings> settings = new HashMap<String, AddressSettings>();
      AddressSettings pagingConfig = new AddressSettings();

      String addressPolicyString =
          candlepinConfig.getString(ConfigProperties.HORNETQ_ADDRESS_FULL_POLICY);
      long maxQueueSizeInMb = candlepinConfig.getInt(ConfigProperties.HORNETQ_MAX_QUEUE_SIZE);
      long maxPageSizeInMb = candlepinConfig.getInt(ConfigProperties.HORNETQ_MAX_PAGE_SIZE);

      AddressFullMessagePolicy addressPolicy = null;
      if (addressPolicyString.equals("PAGE")) {
        addressPolicy = AddressFullMessagePolicy.PAGE;
      } else if (addressPolicyString.equals("BLOCK")) {
        addressPolicy = AddressFullMessagePolicy.BLOCK;
      } else {
        throw new IllegalArgumentException(
            "Unknown HORNETQ_ADDRESS_FULL_POLICY: "
                + addressPolicyString
                + " . Please use one of: PAGE, BLOCK");
      }

      // Paging sizes need to be converted to bytes
      pagingConfig.setMaxSizeBytes(maxQueueSizeInMb * FileUtils.ONE_MB);
      if (addressPolicy == AddressFullMessagePolicy.PAGE) {
        pagingConfig.setPageSizeBytes(maxPageSizeInMb * FileUtils.ONE_MB);
      }
      pagingConfig.setAddressFullMessagePolicy(addressPolicy);
      // Enable for all the queues
      settings.put("#", pagingConfig);
      config.setAddressesSettings(settings);

      int maxScheduledThreads =
          candlepinConfig.getInt(ConfigProperties.HORNETQ_MAX_SCHEDULED_THREADS);
      int maxThreads = candlepinConfig.getInt(ConfigProperties.HORNETQ_MAX_THREADS);
      if (maxThreads != -1) {
        config.setThreadPoolMaxSize(maxThreads);
      }

      if (maxScheduledThreads != -1) {
        config.setScheduledThreadPoolMaxSize(maxScheduledThreads);
      }

      /**
       * Anything up to size of LARGE_MSG_SIZE may be needed to be written to the Journal, so we
       * must set buffer size accordingly.
       *
       * <p>If buffer size would be < LARGE_MSG_SIZE we may get exceptions such as this: Can't write
       * records bigger than the bufferSize(XXXYYY) on the journal
       */
      int largeMsgSize = candlepinConfig.getInt(ConfigProperties.HORNETQ_LARGE_MSG_SIZE);
      config.setJournalBufferSize_AIO(largeMsgSize);
      config.setJournalBufferSize_NIO(largeMsgSize);

      hornetqServer = new EmbeddedHornetQ();
      hornetqServer.setConfiguration(config);
    }
    try {
      hornetqServer.start();
      log.info("Hornetq server started");
    } catch (Exception e) {
      log.error("Failed to start hornetq message server:", e);
      throw new RuntimeException(e);
    }

    setupAmqp(injector, candlepinConfig);
    cleanupOldQueues();

    List<String> listeners = getHornetqListeners(candlepinConfig);

    eventSource = injector.getInstance(EventSource.class);
    for (int i = 0; i < listeners.size(); i++) {
      try {
        Class<?> clazz = this.getClass().getClassLoader().loadClass(listeners.get(i));
        eventSource.registerListener((EventListener) injector.getInstance(clazz));
      } catch (Exception e) {
        log.warn("Unable to register listener " + listeners.get(i), e);
      }
    }

    // Initialize the Event sink AFTER the internal server has been
    // created and started.
    EventSink sink = injector.getInstance(EventSink.class);
    try {
      sink.initialize();
    } catch (Exception e) {
      log.error("Failed to initialize EventSink:", e);
      throw new RuntimeException(e);
    }
  }
Esempio n. 14
0
 public SimpleEventListener(EventSource source) {
   source.registerListener(this);
   this.state = 42;
 }