Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Message delimiter is added several times if message payload exceeds 8192 when using jersey #590

Closed
xenster86 opened this issue Sep 2, 2012 · 9 comments

Comments

@xenster86
Copy link

Found an interesting issue.
Jersey uses BufferedWriter behind the scenes to write the message. If message payload exceeds 8192 (which is default defaultCharBufferSize in BufferedWriter) then postPayload in MessageLengthInterceptor is invoked several times for each 8192 bytes of the message. As result separator is added several times in the middle of message.

@xenster86
Copy link
Author

The problem is in com.sun.jersey.core.util.ReaderWriter#writeToAsString. Even if you override BUFFER_SIZE using system property, it will still use default buffer size in writeToAsString method. Need to override the whole class in the classpath...

@jfarcand
Copy link
Member

jfarcand commented Sep 2, 2012

I think the issue is in the interceptor itself, e.g a property needs to be added that check the size of the chunk and buffer it until Jersey send a smaller chunk. I will investigate...

@xenster86
Copy link
Author

I will try to remove MessageLengthInterceptor and manually add the delimiter at message level

@xenster86
Copy link
Author

So, the following workaround works:
broadcaster.broadcast(content + messageDelimiter)

@jfarcand
Copy link
Member

jfarcand commented Sep 2, 2012

Just to make sure, try with the TrackMessageLengthInterceptor (not the MessageLengthInterceptor). Also the workaround will work as well using the TrackMessageLengthFilter. Thanks!

@jfarcand
Copy link
Member

jfarcand commented Sep 2, 2012

Documented: https://github.com/Atmosphere/atmosphere/wiki/Multiple-messages-arrive-as-single-response-body-or-message-received-are-incomplete A custon interceptor can always be created to buffer the response, but it is recommended to use the BroadcastFilter instead.

@bcopy
Copy link

bcopy commented Dec 17, 2013

Hello Jean-Francois,

I'm afraid http://goo.gl/T1yeq now points to a non-existent page (github shows a "CREATE NEW PAGE" dialog) - can you restore that link so we can give a it a try ?

@profes
Copy link

profes commented Jan 27, 2014

Hi,
In my case the problem was not caused by Jersey's 8192 buffer size. I tried to set it to 32K and 64K and it still was splitting my messages to 8K frames.
I fixed the problem by copying com.sun.jersey.core.util.ReaderWriter and putting it first on the class path.
My ReaderWrited class had a modified writeToAsString(String s, OutputStream out, MediaType type) method, that doesn't use BufferedWriter and OutuputStreamWriter. The latter one falls back to sun.nio.cs.StreamEncoder which has DEFAULT_BYTE_BUFFER_SIZE = 8192, which can't be overridden at all.

Before the change:
com.sun.jersey.core.util.ReaderWriter
public static final void writeToAsString(String s, OutputStream out,
MediaType type) throws IOException {
Writer osw = new BufferedWriter(new OutputStreamWriter(out,
getCharset(type)));
osw.write(s);
osw.flush();
}

After the change:
com.sun.jersey.core.util.ReaderWriter
public static final void writeToAsString(String s, OutputStream out,
MediaType type) throws IOException {
out.write(s.getBytes(getCharset(type)));
out.flush();
}
https://gist.github.com/profes/8645032

I hope that saves you hours of debugging and wondering why all messages above 8K length are chunked.

@bcopy
Copy link

bcopy commented Jun 26, 2014

profes : Has your fix been filed as an issue against JERSEY ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants