ch.qos.logback.core.spi.DeferredProcessingAware Java Examples

The following examples show how to use ch.qos.logback.core.spi.DeferredProcessingAware. 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: AbstractBufferedJedisWriter.java    From logback-redis with Apache License 2.0 6 votes vote down vote up
AbstractBufferedJedisWriter(JedisClient client,
        Function<DeferredProcessingAware, String> messageCreator,
        String redisKey,
        int maxBufferItems,
        long flushBufferIntervalMillis) {
    log = LoggerFactory.getLogger(getClass());

    this.messageCreator = messageCreator;
    this.client = client;
    this.redisKey = redisKey;
    this.maxBufferItems = maxBufferItems;
    this.flushBufferIntervalMillis = flushBufferIntervalMillis;
    shutdown = false;
    bufferedEvents = new LinkedBlockingQueue<>();
    lastFlushEpochMillis = new AtomicLong(System.currentTimeMillis());

    bufferFlusher = createThread(this::flushPeriodically, getClass().getSimpleName(), true);
    bufferFlusher.start();
}
 
Example #2
Source File: BufferedJedisPublisherIT.java    From logback-redis with Apache License 2.0 6 votes vote down vote up
@Test
public void published_messages_are_sent() throws InterruptedException {
    JedisClientProvider clientProvider = mock(JedisClientProvider.class);
    when(clientProvider.getJedisClient()).thenReturn(Optional.of(redisSender));
    final JedisClient jedisClient = new JedisClient(clientProvider, 1, 0L);

    try (BufferedJedisPublisher publisher = new BufferedJedisPublisher(jedisClient, MESSAGE_CREATOR, CHANNEL, 1, BUFFER_FLUSH_MILLIS)) {
        final CountDownLatch receiverStarted = new CountDownLatch(1);
        final CountDownLatch messagesReceived = new CountDownLatch(EVENTS.size());
        final ValueReceiver valueReceiver = new ValueReceiver(redisSubscriber, receiverStarted, messagesReceived);

        final Thread receiverThread = new Thread(valueReceiver);
        receiverThread.start();
        receiverStarted.await();

        final Set<String> sentMessages = new HashSet<>(EVENTS.size());
        for (DeferredProcessingAware event : EVENTS) {
            publisher.append(event);
            sentMessages.add(MESSAGE_CREATOR.apply(event));
        }
        messagesReceived.await(5 * BUFFER_FLUSH_MILLIS, TimeUnit.MILLISECONDS);
        valueReceiver.unsubscribe();

        assertThat(valueReceiver.receivedMessages, is(equalTo(sentMessages)));
    }
}
 
Example #3
Source File: BufferedJedisRPusherTest.java    From logback-redis with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
    MockitoAnnotations.initMocks(this);
    when(messageCreator.apply(Matchers.any())).thenAnswer(invocation -> String.valueOf(invocation.getArgumentAt(0, DeferredProcessingAware.class)));
    final Optional<Pipeline> defaultPipeline = Optional.of(pipeline);
    when(client.getPipeline()).thenReturn(defaultPipeline);

    writer = new BufferedJedisRPusher(client, messageCreator, KEY, DEFAULT_QUEUE_ITEMS, DEFAULT_BATCH_WAIT_MILLIS);
}
 
Example #4
Source File: BufferedJedisPublisherTest.java    From logback-redis with Apache License 2.0 5 votes vote down vote up
@Test
public void dont_send_on_too_many_failures() throws InterruptedException {
    when(pipeline.publish(anyString(), anyString())).thenThrow(new JedisConnectionException(""));
    for (int i = 0; i < DEFAULT_BUFFER_ITEMS; i++) {
        writer.append(mock(DeferredProcessingAware.class));
    }
    verify(client, times(2)).getPipeline();
    verify(client, times(2)).reconnect();
    verify(pipeline, times(2)).publish(anyString(), anyString());
    verify(pipeline, times(0)).sync();
}
 
Example #5
Source File: BufferedJedisPublisherTest.java    From logback-redis with Apache License 2.0 5 votes vote down vote up
@Test
public void send_on_second_try_due_to_exception_on_first_publish() throws InterruptedException {
    when(pipeline.publish(anyString(), anyString())).thenThrow(new JedisConnectionException("")).thenReturn(null);
    for (int i = 0; i < DEFAULT_BUFFER_ITEMS; i++) {
        writer.append(mock(DeferredProcessingAware.class));
    }
    final int expectedPublishCalls = 1 + DEFAULT_BUFFER_ITEMS; // one failed, then for each buffer item
    verify(client, times(2)).getPipeline();
    verify(client).reconnect();
    verify(pipeline, times(expectedPublishCalls)).publish(anyString(), anyString());
    verify(pipeline, times(1)).sync();
}
 
Example #6
Source File: BufferedJedisPublisherTest.java    From logback-redis with Apache License 2.0 5 votes vote down vote up
@Test
public void send_on_second_try_due_to_no_pipline_on_first_try() throws InterruptedException {
    when(client.getPipeline()).thenReturn(Optional.empty()).thenReturn(Optional.of(pipeline));
    for (int i = 0; i < DEFAULT_BUFFER_ITEMS; i++) {
        writer.append(mock(DeferredProcessingAware.class));
    }
    verify(client, times(2)).getPipeline();
    verify(client).reconnect();
    verify(pipeline, times(DEFAULT_BUFFER_ITEMS)).publish(anyString(), anyString());
    verify(pipeline, times(1)).sync();
}
 
Example #7
Source File: BufferedJedisPublisherTest.java    From logback-redis with Apache License 2.0 5 votes vote down vote up
@Test
public void send_on_full_queue() throws InterruptedException {
    int batchFullEvents = 5;
    for (int i = 0; i < DEFAULT_BUFFER_ITEMS * batchFullEvents; i++) {
        writer.append(mock(DeferredProcessingAware.class));
    }
    final int expectedPublishCalls = DEFAULT_BUFFER_ITEMS * batchFullEvents; // one per item
    verify(pipeline, times(expectedPublishCalls)).publish(anyString(), anyString());
    verify(pipeline, times(batchFullEvents)).sync();
}
 
Example #8
Source File: BufferedJedisPublisherTest.java    From logback-redis with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
    MockitoAnnotations.initMocks(this);
    when(messageCreator.apply(Matchers.any())).thenAnswer(invocation -> String.valueOf(invocation.getArgumentAt(0, DeferredProcessingAware.class)));
    final Optional<Pipeline> defaultPipeline = Optional.of(pipeline);
    when(client.getPipeline()).thenReturn(defaultPipeline);

    writer = new BufferedJedisPublisher(client, messageCreator, KEY, DEFAULT_BUFFER_ITEMS, DEFAULT_BATCH_WAIT_MILLIS);
}
 
Example #9
Source File: BufferedJedisRPusherTest.java    From logback-redis with Apache License 2.0 5 votes vote down vote up
@Test
public void dont_send_on_too_many_failures() throws InterruptedException {
    when(pipeline.rpush(anyString(), anyVararg())).thenThrow(new JedisConnectionException(""));
    for (int i = 0; i < DEFAULT_QUEUE_ITEMS; i++) {
        writer.append(mock(DeferredProcessingAware.class));
    }
    verify(client, times(2)).getPipeline();
    verify(client, times(2)).reconnect();
    verify(pipeline, times(2)).rpush(anyString(), anyVararg());
    verify(pipeline, times(0)).sync();
}
 
Example #10
Source File: BufferedJedisRPusherTest.java    From logback-redis with Apache License 2.0 5 votes vote down vote up
@Test
public void send_on_second_try_due_to_exception_on_first_rpush() throws InterruptedException {
    when(pipeline.rpush(anyString(), anyVararg())).thenThrow(new JedisConnectionException("")).thenReturn(null);
    for (int i = 0; i < DEFAULT_QUEUE_ITEMS; i++) {
        writer.append(mock(DeferredProcessingAware.class));
    }
    verify(client, times(2)).getPipeline();
    verify(client).reconnect();
    verify(pipeline, times(2)).rpush(anyString(), anyVararg());
    verify(pipeline, times(1)).sync();
}
 
Example #11
Source File: BufferedJedisRPusherTest.java    From logback-redis with Apache License 2.0 5 votes vote down vote up
@Test
public void send_on_second_try_due_to_no_pipline_on_first_try() throws InterruptedException {
    when(client.getPipeline()).thenReturn(Optional.empty()).thenReturn(Optional.of(pipeline));
    for (int i = 0; i < DEFAULT_QUEUE_ITEMS; i++) {
        writer.append(mock(DeferredProcessingAware.class));
    }
    verify(client, times(2)).getPipeline();
    verify(client).reconnect();
    verify(pipeline, times(1)).rpush(anyString(), anyVararg());
    verify(pipeline, times(1)).sync();
}
 
Example #12
Source File: BufferedJedisRPusherTest.java    From logback-redis with Apache License 2.0 5 votes vote down vote up
@Test
public void send_on_full_queue() throws InterruptedException {
    int batchFullEvents = 5;
    for (int i = 0; i < DEFAULT_QUEUE_ITEMS * batchFullEvents; i++) {
        writer.append(mock(DeferredProcessingAware.class));
    }
    verify(pipeline, times(batchFullEvents)).rpush(anyString(), anyVararg());
    verify(pipeline, times(batchFullEvents)).sync();
}
 
Example #13
Source File: ServicePropertiesProvider.java    From ja-micro with Apache License 2.0 5 votes vote down vote up
@Override
public void writeTo(JsonGenerator generator, DeferredProcessingAware deferredProcessingAware) throws IOException {
    generator.writeFieldName("service");
    generator.writeString(serviceName);
    generator.writeFieldName("service-version");
    generator.writeString(serviceVersion);
    generator.writeFieldName("service-id");
    generator.writeString(serviceInstanceId);
}
 
Example #14
Source File: BufferedJedisPublisher.java    From logback-redis with Apache License 2.0 5 votes vote down vote up
BufferedJedisPublisher(JedisClient client,
        Function<DeferredProcessingAware, String> messageCreator,
        String redisKey,
        int maxBufferItems,
        long flushBufferIntervalMillis) {
    super(client, messageCreator, redisKey, maxBufferItems, flushBufferIntervalMillis);
}
 
Example #15
Source File: BufferedJedisRPusher.java    From logback-redis with Apache License 2.0 5 votes vote down vote up
BufferedJedisRPusher(JedisClient client,
        Function<DeferredProcessingAware, String> messageCreator,
        String redisKey,
        int maxBufferItems,
        long flushBufferIntervalMillis) {
    super(client, messageCreator, redisKey, maxBufferItems, flushBufferIntervalMillis);
}
 
Example #16
Source File: BufferedJedisWriterFactory.java    From logback-redis with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("squid:S2095") // rule=resource should be closed: JedisClient is closed when writer is shut down
public AbstractBufferedJedisWriter createJedisWriter(JedisWriterConfiguration writerConfiguration) {
    final RedisConnectionConfig connectionConfig = writerConfiguration.getConnectionConfig();
    final Method method = connectionConfig.getMethod();
    if (method == null) {
        throw getUnsupportedWriterTypeException(null);
    }
    final Encoder<DeferredProcessingAware> encoder = writerConfiguration.getEncoder();
    final Function<DeferredProcessingAware, String> messageCreator = event -> new String(encoder.encode(event), StandardCharsets.UTF_8);

    final JedisClientProvider clientProvider = new JedisClientProvider(jedisPoolFactory, writerConfiguration.getConnectionConfig());
    final JedisClient client = new JedisClient(clientProvider,
            writerConfiguration.getMaxInitializeTries(),
            writerConfiguration.getRetryInitializeIntervalMillis());

    switch (method) {
        case RPUSH:
            return new BufferedJedisRPusher(client,
                    messageCreator,
                    connectionConfig.getKey(),
                    writerConfiguration.getMaxBufferedMessages(),
                    writerConfiguration.getFlushBufferIntervalMillis());
        case PUBLISH:
            return new BufferedJedisPublisher(client,
                    messageCreator,
                    connectionConfig.getKey(),
                    writerConfiguration.getMaxBufferedMessages(),
                    writerConfiguration.getFlushBufferIntervalMillis());
        default:
            throw getUnsupportedWriterTypeException(method.name());
    }
}
 
Example #17
Source File: AbstractBufferedJedisWriter.java    From logback-redis with Apache License 2.0 5 votes vote down vote up
private void flushBuffer() {
    try {
        final List<DeferredProcessingAware> toPush = new ArrayList<>(bufferedEvents.size());
        bufferedEvents.drainTo(toPush);
        final String[] values = toPush.stream().map(messageCreator).toArray(String[]::new);
        for (int i = 1; i <= SEND_EVENT_TRIES; i++) {
            if (sendValuesToRedis(values)) {
                return;
            }
        }
        log.warn("unable to send events to redis: {}", Arrays.asList(values));
    } finally {
        lastFlushEpochMillis.set(System.currentTimeMillis());
    }
}
 
Example #18
Source File: AbstractBufferedJedisWriter.java    From logback-redis with Apache License 2.0 5 votes vote down vote up
public void append(DeferredProcessingAware event) {
    if (event != null && !bufferedEvents.offer(event)) {
        final String encodedEvent = messageCreator.apply(event);
        log.warn("unable to add event {} to buffer", encodedEvent);
    }
    if (maxBatchSizeReached() || maxBatchWaitTimeReached()) {
        flushBuffer();
    }
}
 
Example #19
Source File: RedisBatchAppender.java    From logback-redis with Apache License 2.0 4 votes vote down vote up
public void setEncoder(Encoder<DeferredProcessingAware> encoder) {
    this.encoder = encoder;
}
 
Example #20
Source File: RedisBatchAppender.java    From logback-redis with Apache License 2.0 4 votes vote down vote up
@Override
protected void append(DeferredProcessingAware event) {
    writer.append(event);
}
 
Example #21
Source File: LogzioLogbackAppenderTest.java    From logzio-logback-appender with Apache License 2.0 4 votes vote down vote up
private <T extends DeferredProcessingAware> AbstractFieldJsonProvider<T> withName(String name,
                                                                                  AbstractFieldJsonProvider<T> provider) {
    provider.setFieldName(name);
    return provider;
}