Further, I do have a nasty hack as follows, which I really really don't 
want to use:

private static void rescueJsonGeneratorState(ByteBuffer byteBuffer, 
JsonGenerator jsonGenerator) {
    List<CheckedRunnable> rescueAction = Arrays.asList(
            () -> jsonGenerator.writeNumber(0),     // Try completing a missing 
field value.
            jsonGenerator::writeEndArray,           // Try closing an array.
            jsonGenerator::writeEndObject);         // Try closing an object.
    for (int rescueActionId = 0;; rescueActionId = ++rescueActionId % 
rescueAction.size()) {
        byteBuffer.clear();
        try {
            jsonGenerator.writeStartObject();
            jsonGenerator.writeEndObject();
            jsonGenerator.flush();
            return;
        } catch (IOException error) {
            if (error instanceof JsonGenerationException) {
                try {
                    rescueAction.get(rescueActionId).run();
                } catch (Throwable ignored) {
                    // Ignored.
                }
            } else {
                throw new RuntimeException(error);
            }
        }
    }
}



On Wednesday, October 24, 2018 at 2:35:48 PM UTC+2, Volkan Yazıcı wrote:
>
> I am at the edge of making log4j2-logstash-layout 
> <https://github.com/vy/log4j2-logstash-layout> gc-free. And at
> this stage my only (GC) obstacle is the aforementioned two classes.
> I need to instantiate them at each encode(LogEvent) call and this
> spoils the entire gc-free magic.
>
> What I can do is create a ThreadLocal parade of 
> ByteBufferOutputStream->JsonGenerator->FilteringGeneratorDelegate.
> This allows me to implement encode() as follows:
>
>    1. get ThreadLocal resources
>    2. reset BBOS
>    3. writeStartObject()
>    4. encode fields
>    5. writeEndObject()
>    6. flip and drain the BBOS to the Log4j BB sink
>
> Though this approach has an unfortunate showstopper: If the 4th step
> leaves the JsonGenerator and/or the FilteringGeneratorDelegate in a
> dirty state -- consider exceeding BBOS capacity, etc. -- all the
> subsequent calls will fail.
>
> I will deeply appreciate any kind of help on this issue.
>
> Best.
>
> On Monday, October 22, 2018 at 6:59:02 AM UTC+2, Tatu Saloranta wrote:
>>
>> On Sun, Oct 21, 2018 at 2:02 PM Volkan Yazıcı <[email protected]> 
>> wrote: 
>> > 
>> > Hi, 
>> > 
>> > I have a JsonGenerator and FilteringGeneratorDelegate usage as follows: 
>> > 
>> > ByteArrayOutputStream outputStream = threadLocalOutputStreamRef.get(); 
>> > try (JsonGenerator jsonGenerator = 
>> jsonFactory.createGenerator(outputStream)) { 
>> >     try (JsonGenerator jsonGeneratorDelegate = new 
>> FilteringGeneratorDelegate(jsonGenerator, tokenFilter, true, true)) { 
>> >         // ... 
>> >     } 
>> > } 
>> > 
>> > Is it possible to reuse JsonGenerator and FilteringGeneratorDelegate 
>> instances in a way attached to the local thread context? 
>>
>> No, JsonGenerator is designed for use-once life-cycle and does not 
>> support reuse. 
>> Instances are light-weight so there shouldn't usually be much benefit 
>> from attempting to reuse instances; same goes with 
>> FilteringGeneratorDelegate. 
>>
>> -+ Tatu +- 
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to