コード例 #1
0
ファイル: RestTests.java プロジェクト: lxl514614/qbit
  @Test
  public void testBadJSONCustomHandler() {

    serviceEndpointServer.stop();

    openPort = PortUtils.findOpenPort();
    serviceEndpointServer =
        EndpointServerBuilder.endpointServerBuilder()
            .setPort(openPort)
            .setErrorHandler(
                new Consumer<Throwable>() {
                  @Override
                  public void accept(Throwable throwable) {
                    final Optional<HttpRequest> httpRequest = new HttpContext().getHttpRequest();
                    if (httpRequest.isPresent()) {
                      httpRequest
                          .get()
                          .getReceiver()
                          .respondOK("\"Bad JSON" + throwable.getMessage() + "\"");
                      httpRequest.get().handled();
                    }
                  }
                })
            .build();
    serviceEndpointServer.initServices(new TestService());
    serviceEndpointServer.startServerAndWait();

    HTTP.Response response =
        HTTP.jsonRestCallViaPOST(
            buildURL("addall"),
            "\"i\": 1, \"s\": \"string\"}, " + "{\"i\": 2, \"s\": \"string2\"}]");

    assertEquals(200, response.status());
  }
コード例 #2
0
ファイル: RestTests.java プロジェクト: lxl514614/qbit
 @Before
 public void before() {
   openPort = PortUtils.findOpenPort();
   serviceEndpointServer = EndpointServerBuilder.endpointServerBuilder().setPort(openPort).build();
   serviceEndpointServer.initServices(new TestService());
   serviceEndpointServer.startServerAndWait();
 }
コード例 #3
0
ファイル: TestHeaderFidelity.java プロジェクト: bsa01/qbit
  @Test
  public void test() throws Exception {

    final int openPortStartAt = PortUtils.findOpenPortStartAt(7777);
    HttpServer server = HttpServerBuilder.httpServerBuilder().setPort(openPortStartAt).build();

    server.setHttpRequestConsumer(
        serverRequest -> {
          final MultiMap<String, String> headers = MultiMap.multiMap();

          headers.add("foo", "bar").add("foo", "baz");
          serverRequest.getReceiver().response(200, "application/json", "true", headers);
        });

    server.startServer();

    HttpClient client =
        HttpClientBuilder.httpClientBuilder().setPort(openPortStartAt).setHost("localhost").build();

    client.start();

    final HttpTextResponse httpResponse = client.get("/hi");

    Sys.sleep(1000);
    puts(httpResponse.headers());

    boolean foundFoo = httpResponse.headers().keySet().contains("foo");
    assertTrue(foundFoo);
  }
  public static void main(String... args) throws Exception {

    final BrokerService broker; // JMS Broker to make this a self contained example.
    final int port; // port to bind to JMS Broker to

    /* ******************************************************************************/
    /* START JMS BROKER. ************************************************************/
    /* Start up JMS Broker. */
    port = PortUtils.findOpenPortStartAt(4000);
    broker = new BrokerService();
    broker.addConnector("tcp://localhost:" + port);
    broker.start();

    Sys.sleep(5_000);

    /* ******************************************************************************/
    /* START JMS CLIENTS FOR SERVER A AND B *******************************************/
    /* Create a JMS Builder to create JMS Queues. */
    final JmsServiceBuilder jmsBuilder =
        JmsServiceBuilder.newJmsServiceBuilder()
            .setPort(port)
            .setDefaultDestination(NEW_HIRE_CHANNEL)
            .setAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE);

    /* JMS client for server A. */
    final JsonQueue<Employee> employeeJsonQueueServerA =
        new JsonQueue<>(Employee.class, new JmsTextQueue(jmsBuilder));

    /* JMS client for server B. */
    final JsonQueue<Employee> employeeJsonQueueServerB =
        new JsonQueue<>(Employee.class, new JmsTextQueue(jmsBuilder));

    /* Send Queue to send messages to JMS broker. */
    final SendQueue<Employee> sendQueueA = employeeJsonQueueServerA.sendQueue();
    Sys.sleep(1_000);

    /*  ReceiveQueueB Queue B to receive messages from JMS broker. */
    final ReceiveQueue<Employee> receiveQueueB = employeeJsonQueueServerB.receiveQueue();
    Sys.sleep(1_000);

    /* ******************************************************************************/
    /* START EVENT BUS A  ************************************************************/
    /* Create you own private event bus for Server A. */
    final EventManager privateEventBusServerAInternal =
        EventManagerBuilder.eventManagerBuilder()
            .setEventConnector(
                new EventConnector() {
                  @Override
                  public void forwardEvent(EventTransferObject<Object> event) {

                    if (event.channel().equals(NEW_HIRE_CHANNEL)) {
                      System.out.println(event);
                      final Object body = event.body();
                      final Object[] bodyArray = ((Object[]) body);
                      final Employee employee = (Employee) bodyArray[0];
                      System.out.println(employee);
                      sendQueueA.sendAndFlush(employee);
                    }
                  }
                })
            .setName("serverAEventBus")
            .build();

    /* Create a service queue for this event bus. */
    final ServiceQueue privateEventBusServiceQueueA =
        serviceBuilder()
            .setServiceObject(privateEventBusServerAInternal)
            .setInvokeDynamic(false)
            .build();

    final EventManager privateEventBusServerA =
        privateEventBusServiceQueueA.createProxyWithAutoFlush(
            EventManager.class, 50, TimeUnit.MILLISECONDS);

    /* Create you own private event bus for Server B. */
    final EventManager privateEventBusServerBInternal =
        EventManagerBuilder.eventManagerBuilder()
            .setEventConnector(
                new EventConnector() {
                  @Override
                  public void forwardEvent(EventTransferObject<Object> event) {
                    System.out.println(event);
                    final Object body = event.body();
                    final Object[] bodyArray = ((Object[]) body);
                    final Employee employee = (Employee) bodyArray[0];
                    System.out.println(employee);
                    sendQueueA.sendAndFlush(employee);
                  }
                })
            .setName("serverBEventBus")
            .build();

    /* Create a service queue for this event bus. */
    final ServiceQueue privateEventBusServiceQueueB =
        serviceBuilder()
            .setServiceObject(privateEventBusServerBInternal)
            .setInvokeDynamic(false)
            .build();

    final EventManager privateEventBusServerB =
        privateEventBusServiceQueueB.createProxyWithAutoFlush(
            EventManager.class, 50, TimeUnit.MILLISECONDS);

    final EventBusProxyCreator eventBusProxyCreator = QBit.factory().eventBusProxyCreator();

    final EmployeeEventManager employeeEventManagerA =
        eventBusProxyCreator.createProxy(privateEventBusServerA, EmployeeEventManager.class);

    final EmployeeEventManager employeeEventManagerB =
        eventBusProxyCreator.createProxy(privateEventBusServerB, EmployeeEventManager.class);

    // This did not work. Not sure why?
    //        /* ******************************************************************************/
    //        /* LISTEN TO JMS CLIENT B and FORWARD to Event bus. **********************/
    //        /* Listen to JMS client and push to B event bus ****************************/
    //        employeeJsonQueueServerB.startListener(new ReceiveQueueListener<Employee>(){
    //            @Override
    //            public void receive(final Employee employee) {
    //                System.out.println("HERE " + employee);
    //                employeeEventManagerB.sendNewEmployee(employee);
    //                System.out.println("LEFT " + employee);
    //
    //            }
    //        });

    final PeriodicScheduler periodicScheduler = QBit.factory().createPeriodicScheduler(1);
    periodicScheduler.repeat(
        new Runnable() {
          @Override
          public void run() {
            final Employee employee = receiveQueueB.pollWait();
            if (employee != null) {
              employeeEventManagerB.sendNewEmployee(employee);
            }
          }
        },
        50,
        TimeUnit.MILLISECONDS);

    final SalaryChangedChannel salaryChangedChannel =
        eventBusProxyCreator.createProxy(privateEventBusServerA, SalaryChangedChannel.class);

    /*
    Create your EmployeeHiringService but this time pass the private event bus.
    Note you could easily use Spring or Guice for this wiring.
     */
    final EmployeeHiringService employeeHiring =
        new EmployeeHiringService(employeeEventManagerA, salaryChangedChannel); // Runs on Server A

    /* Now create your other service POJOs which have no compile time dependencies on QBit. */
    final PayrollService payroll = new PayrollService(); // Runs on Server A
    final BenefitsService benefits = new BenefitsService(); // Runs on Server A

    final VolunteerService volunteering = new VolunteerService(); // Runs on Server B

    /** Employee hiring service. A. */
    ServiceQueue employeeHiringServiceQueue =
        serviceBuilder().setServiceObject(employeeHiring).setInvokeDynamic(false).build();

    /** Payroll service A. */
    ServiceQueue payrollServiceQueue =
        serviceBuilder().setServiceObject(payroll).setInvokeDynamic(false).build();

    /** Employee Benefits service. A. */
    ServiceQueue employeeBenefitsServiceQueue =
        serviceBuilder().setServiceObject(benefits).setInvokeDynamic(false).build();

    /** Community outreach program. B. */
    ServiceQueue volunteeringServiceQueue =
        serviceBuilder().setServiceObject(volunteering).setInvokeDynamic(false).build();

    /* Now wire in the event bus so it can fire events into the service queues.
     * For ServerA. */
    privateEventBusServerA.joinService(payrollServiceQueue);
    privateEventBusServerA.joinService(employeeBenefitsServiceQueue);

    /* Now wire in event B bus. */
    privateEventBusServerB.joinService(volunteeringServiceQueue);

    /* Start Server A bus. */
    privateEventBusServiceQueueA.start();

    /* Start Server B bus. */
    privateEventBusServiceQueueB.start();

    employeeHiringServiceQueue.start();
    volunteeringServiceQueue.start();
    payrollServiceQueue.start();
    employeeBenefitsServiceQueue.start();

    /** Now create the service proxy like before. */
    EmployeeHiringServiceClient employeeHiringServiceClientProxy =
        employeeHiringServiceQueue.createProxy(EmployeeHiringServiceClient.class);

    /** Call the hireEmployee method which triggers the other events. */
    employeeHiringServiceClientProxy.hireEmployee(new Employee("Lucas", 1));

    flushServiceProxy(employeeHiringServiceClientProxy);

    Sys.sleep(5_000);
  }