Ejemplo n.º 1
0
  @RequestMapping(method = RequestMethod.GET, value = "/{regionId}/warehouses/{warehouseId}")
  public @ResponseBody ApiResponse<List<Warehouse>> getWarehouseList(
      @PathVariable("regionId") String id, @PathVariable("warehouseId") String warehouseId) {
    List<Warehouse> warehouseList = new ArrayList<Warehouse>();
    ApiResponse<List<Warehouse>> apiResponse = new ApiResponse<List<Warehouse>>();

    logger.debug(
        " WarehouseController [ getWarehouseList ] - Returning List of Warehouses for the regionId {} ",
        id);
    warehouseList = warehouseService.getWarehouseList(new Long(id), new Long(warehouseId));

    if (!CollectionUtils.isEmpty(warehouseList)) {
      apiResponse.setData(warehouseList);

      for (Warehouse w : CollectionsUtilService.nullGuard(warehouseList)) {
        logger.debug(" WarehouseName - {} ", w.getWarehouseName());
        logger.debug(" WarehouseId   - {} ", w.getWarehouseId());
      }
    }

    return apiResponse;
  }
  @Test
  public void testPublishAndSubscribeBroadCastServiceWithSeparateThread() throws Exception {
    final Random random = new Random(System.currentTimeMillis());

    final class LucasBroadCastServiceThread extends Thread {

      private LucasMessageBroadcastService lucasMessageBroadcastService;
      final String randomString = UUID.randomUUID().toString();
      String queueName;
      String bindKey;
      String data;

      public LucasBroadCastServiceThread(
          final LucasMessageBroadcastService lucasMessageBroadcastService,
          final String threadName,
          final int priority)
          throws Exception {

        super(threadName);
        super.setPriority(priority);
        this.lucasMessageBroadcastService = lucasMessageBroadcastService;
        final int randomNum = random.nextInt(10) + 1;
        this.queueName = TEST_QUEUE_PREFIX + randomNum;
        this.bindKey = TEST_QUEUE_PREFIX + ".Binding" + randomNum;
        this.data = randomString;
      }

      @Override
      public void run() {
        try {
          LOG.debug("Posting queueName: " + queueName + " and data: " + data);
          pushedQueueMap.put(queueName, data);
          this.lucasMessageBroadcastService.sendMessage(queueName, bindKey, data);
        } catch (Exception e) {
          LOG.error("Exception Generated ", e);
          throw new LucasRuntimeException(e);
        }
      }
    }

    final class LucasBroadCastClientThread extends Thread {

      private LucasMessageBroadcastClientService lucasMessageBroadcastClientService;
      private String lucasBroadCastQueue;
      private String lucasBroadCastBinding;

      public LucasBroadCastClientThread(
          LucasMessageBroadcastClientService lucasMessageBroadcastClientService,
          String threadName,
          int threadPriority,
          String lucasBroadCastQueue,
          String lucasBroadCastBinding) {
        super(threadName);
        super.setPriority(threadPriority);
        this.lucasMessageBroadcastClientService = lucasMessageBroadcastClientService;
        this.lucasBroadCastBinding = lucasBroadCastBinding;
        this.lucasBroadCastQueue = lucasBroadCastQueue;
      }

      @Override
      public void run() {
        try {
          String[] stringArray =
              this.lucasMessageBroadcastClientService.receiveAllMessagesWithinTimePeriod(
                  this.lucasBroadCastQueue, this.lucasBroadCastBinding, 1L, 5000L);
          if (stringArray != null) {
            for (String string : stringArray) {
              pulledQueueMap.put(this.lucasBroadCastQueue, string);
            }
          }

        } catch (Exception e) {
          LOG.error("Exception Generated ", e);
          throw new LucasRuntimeException(e);
        }
      }
    }

    //
    // Publisher Threads
    final int maxThreads = 1000;
    ExecutorService pushExecutor = Executors.newFixedThreadPool(maxThreads);
    CompletionService<?> pushCompletion = new ExecutorCompletionService(pushExecutor);
    for (int i = 0; i < maxThreads; i++) {
      LucasBroadCastServiceThread thread =
          new LucasBroadCastServiceThread(
              this.lucasBroadCastService, "LucasBroadCastServiceThread" + i, 5);
      pushCompletion.submit(thread, null);
    }
    for (int i = 0; i < maxThreads; ++i) {
      pushCompletion.take();
    }
    pushExecutor.shutdown();

    // Subscriber threads
    int j = 0;
    // Get the queueNames stored while publishing...
    Set<String> keySet = pushedQueueMap.keySet();
    if (keySet != null) {
      ExecutorService pullExecutor = Executors.newFixedThreadPool(keySet.size());
      CompletionService<?> pullCompletion = new ExecutorCompletionService(pullExecutor);
      for (String queueName : keySet) {
        // ...and receive on each queueName
        LucasBroadCastClientThread thread =
            new LucasBroadCastClientThread(
                lucasMessageBroadcastClientService,
                "LucasBroadCastClientThread" + j,
                5,
                queueName,
                TEST_QUEUE_PREFIX + ".Binding" + j);
        pullCompletion.submit(thread, null);
        j++;
      }
      for (int i = 0; i < keySet.size(); ++i) {
        pullCompletion.take();
      }
      pullExecutor.shutdown();
    }

    LOG.debug("Pushed values:");
    CollectionsUtilService.dumpMultiMapNumbers(pushedQueueMap);
    LOG.debug("Pulled values:");
    CollectionsUtilService.dumpMultiMapNumbers(pulledQueueMap);
    // Assert
    junit.framework.Assert.assertTrue(
        "The pushed and pulled values do not match!",
        CollectionsUtilService.compareMultiMaps(pushedQueueMap, pulledQueueMap));
  }