Tuesday, March 05, 2024

subscribeOn or publishOn? may be both.

neither
[ctor-http-nio-4] ServiceClient : getting
[ctor-http-nio-4] Controller : getting
[ctor-http-nio-4] ServiceClient : got
[ctor-http-nio-4] Controller : got


subscribeOn in service
[ctor-http-nio-4] Controller : getting
[ scheduler-1] ServiceClient : getting
[ctor-http-nio-6] ServiceClient : got
[ctor-http-nio-6] Controller : got


publishOn in service client
[ctor-http-nio-4] ServiceClient : getting
[ctor-http-nio-4] Controller : getting
[ scheduler-1] ServiceClient : got
[ scheduler-1] Controller : got


subscribeOn in service and publishOn in service client
[ctor-http-nio-4] Controller : getting
[ scheduler-1] ServiceClient : getting
[ scheduler-2] ServiceClient : got
[ scheduler-2] Controller : got

Sunday, July 30, 2023

how to transfer oversized string over message system

for text based message system, if there's a limit of x KB, you can still transfer (~5 * x) KB by

  1. compressing the original payload
  2. encoding compressed binary to text
  3. sending it
receiver reverses the process to get original payload by
  1. testing to make sure message is encoded
  2. decoding it to compressed binary
  3. uncompressing decoded binary
import org.apache.commons.codec.binary.Base64;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

String payload = """
        {
        }""";
System.out.printf("original size: %s bytes\n", payload.length());

ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream gos = new GZIPOutputStream(baos);
gos.write(payload.getBytes());
gos.close();
byte[] compressed = baos.toByteArray();
System.out.printf("compressed size: %s bytes\n", compressed.length);

String encoded = Base64.encodeBase64String(compressed);
System.out.printf("encoded size: %s bytes\n", encoded.length());

System.out.println("is message encoded? " + Base64.isBase64(encoded));
byte[] decoded = Base64.decodeBase64(encoded.getBytes());
System.out.printf("decoded size: %s bytes\n", decoded.length);

GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(decoded));
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = gis.read(buffer)) > 0) {
    output.write(buffer, 0, bytesRead);
}
gis.close();
String uncompressed = output.toString();
System.out.printf("uncompressed size: %s bytes\n", uncompressed.length());


original size: 9450 bytes
compressed size: 1235 bytes
encoded size: 1648 bytes
is message encoded? true
decoded size: 1235 bytes
uncompressed size: 9450 bytes