java.util.concurrent.TransferQueue Java Examples

The following examples show how to use java.util.concurrent.TransferQueue. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestStreamSetsMessageConsumer.java    From datacollector with Apache License 2.0 6 votes vote down vote up
@Test
public void testConsumerSingleMessage() throws Exception {
  TransferQueue<RabbitMessage> messages = new LinkedTransferQueue<>();

  Channel channel = mock(Channel.class);

  final Consumer consumer = new StreamSetsMessageConsumer(channel, messages);
  final Envelope envelope = new Envelope(1L, false, EXCHANGE_NAME, QUEUE_NAME);

  executor.submit(new Runnable() {
    @Override
    public void run() {
      try {
        consumer.handleDelivery("consumerTag", envelope, null, TEST_MESSAGE_1.getBytes());
      } catch (IOException ignored) {
        // no op
      }
    }
  });

  RabbitMessage message = messages.take();
  assertEquals(TEST_MESSAGE_1, new String(message.getBody(), StandardCharsets.UTF_8));
}
 
Example #2
Source File: EliminationProfile.java    From multiway-pool with Apache License 2.0 6 votes vote down vote up
Runnable newLinkedTransferQueueRunner() {
  final TransferQueue<Integer> queue = new LinkedTransferQueue<>();
  return new Runnable() {
    @Override public void run() {
      final ThreadLocalRandom random = ThreadLocalRandom.current();
      for (;;) {
        if (random.nextBoolean()) {
          queue.offer(ELEMENT);
        } else {
          queue.poll();
        }
        calls.increment();
      }
    }
  };
}
 
Example #3
Source File: Main.java    From Java-Coding-Problems with MIT License 5 votes vote down vote up
public static void main(String[] args) throws InterruptedException {

        System.setProperty("java.util.logging.SimpleFormatter.format",
                "[%1$tT] [%4$-7s] %5$s %n");

        Thread thread = new Thread(() -> {

            TransferQueue<String> queue = new LinkedTransferQueue<>();

            while (!Thread.currentThread().isInterrupted()) {

                try {
                    logger.info(() -> "For 3 seconds the thread "
                            + Thread.currentThread().getName()
                            + " will try to poll an element from queue ...");

                    queue.poll(3000, TimeUnit.MILLISECONDS);
                } catch (InterruptedException ex) {
                    logger.severe(() -> "InterruptedException! The thread "
                            + Thread.currentThread().getName() + " was intrrupted!");
                    Thread.currentThread().interrupt(); // comment this line to see the effect
                }
            }

            logger.info(() -> "The execution was stopped!");
        });

        thread.start();
        Thread.sleep(1500);
        thread.interrupt();
    }
 
Example #4
Source File: ScalingThreadPoolExecutor.java    From Wisp with Apache License 2.0 4 votes vote down vote up
public DynamicBlockingQueue(final TransferQueue<E> delegate) {
	this.delegate = delegate;
}
 
Example #5
Source File: RabbitSource.java    From datacollector with Apache License 2.0 4 votes vote down vote up
TransferQueue<RabbitMessage> getMessageQueue() {
  return messages;
}
 
Example #6
Source File: RabbitSourceTest.java    From datacollector with Apache License 2.0 4 votes vote down vote up
@Test
public void testHeaderProcessing() throws Exception {
  ((RabbitSourceConfigBean)conf).basicConfig.maxWaitTime = 1000; // Set this low so that we don't slow down the test.

  stage = PowerMockito.spy(newStage());

  // setup some fake data and force it onto the source's queue
  RabbitSource source = (RabbitSource)stage;
  TransferQueue<RabbitMessage> messages = source.getMessageQueue();
  Envelope envelope = new Envelope(DELIVERY_TAG, REDELIVERED, EXCHANGE_NAME, QUEUE_NAME);
  AMQP.BasicProperties.Builder propertiesBuilder = new AMQP.BasicProperties.Builder();
  propertiesBuilder.contentType(CONTENT_TYPE);
  Map<String, Object> customHeaders = new HashMap<>();
  customHeaders.put(CUSTOM_HEADER_KEY, CUSTOM_HEADER_VAL);
  propertiesBuilder.headers(customHeaders);
  propertiesBuilder.clusterId(CLUSTER_ID);
  AMQP.BasicProperties properties = propertiesBuilder.build();
  RabbitMessage msg = new RabbitMessage(CONSUMER_TAG, envelope, properties, TEST_MESSAGE_1.getBytes());
  source.getMessageQueue().put(msg);
  doReturn(new ArrayList<Stage.ConfigIssue>()).when((RabbitSource)stage).init();

  PowerMockito.doReturn(false).when(stage, "isConnected");

  this.runner = newStageRunner("output");

  // setup items which are not correctly configured in init
  Channel channel = mock(Channel.class);
  StreamSetsMessageConsumer consumer = new StreamSetsMessageConsumer(channel, messages);
  source.setStreamSetsMessageConsumer(consumer);
  DataParserFactory parserFactory = new DataParserFactoryBuilder(runner.getContext(), DataParserFormat.JSON)
      .setCharset(StandardCharsets.UTF_8)
      .setMode(JsonMode.MULTIPLE_OBJECTS)
      .setMaxDataLen(-1)
      .build();
  source.setDataParserFactory(parserFactory);

  runner.runInit();

  StageRunner.Output output = ((SourceRunner)runner).runProduce(null, 1000);
  List<Record> records = output.getRecords().get("output");
  assertEquals(1, records.size());
  Record record = records.get(0);
  assertEquals(String.valueOf(DELIVERY_TAG), record.getHeader().getAttribute("deliveryTag"));
  assertEquals(String.valueOf(REDELIVERED), record.getHeader().getAttribute("redelivered"));
  assertEquals(EXCHANGE_NAME, record.getHeader().getAttribute("exchange"));
  assertEquals(CONTENT_TYPE, record.getHeader().getAttribute("contentType"));
  assertNull(record.getHeader().getAttribute("appId"));
  assertEquals(CUSTOM_HEADER_VAL, record.getHeader().getAttribute(CUSTOM_HEADER_KEY));
  runner.runDestroy();
}
 
Example #7
Source File: Consumer.java    From tutorials with MIT License 4 votes vote down vote up
Consumer(TransferQueue<String> transferQueue, String name, int numberOfMessagesToConsume) {
    this.transferQueue = transferQueue;
    this.name = name;
    this.numberOfMessagesToConsume = numberOfMessagesToConsume;
}
 
Example #8
Source File: Producer.java    From tutorials with MIT License 4 votes vote down vote up
Producer(TransferQueue<String> transferQueue, String name, Integer numberOfMessagesToProduce) {
    this.transferQueue = transferQueue;
    this.name = name;
    this.numberOfMessagesToProduce = numberOfMessagesToProduce;
}
 
Example #9
Source File: AsyncAppender.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
private boolean transfer(final LogEvent memento) {
    return queue instanceof TransferQueue
        ? ((TransferQueue<LogEvent>) queue).tryTransfer(memento)
        : queue.offer(memento);
}
 
Example #10
Source File: StreamSetsMessageConsumer.java    From datacollector with Apache License 2.0 2 votes vote down vote up
/**
 * Constructs a new instance and records its association to the passed-in channel.
 *
 * @param channel the channel to which this consumer is attached
 */
public StreamSetsMessageConsumer(Channel channel, TransferQueue<RabbitMessage> records) {
  super(channel);
  this.records = records;
}