Skip to content

Commit

Permalink
unsafe goes brrr, no need to make empty constructors in messages anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
CDFN committed Apr 21, 2021
1 parent 572c130 commit 857b09a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ public static class ConfigRequest implements MessagePackSerializable {

private int id;

public ConfigRequest() {
}

public ConfigRequest(int id) {
this.id = id;
}
Expand Down Expand Up @@ -46,9 +43,6 @@ public static class ConfigResponse implements MessagePackSerializable {
private int id;
private String data;

public ConfigResponse() {
}

public ConfigResponse(int id, String data) {
this.id = id;
this.data = data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,23 @@
import io.lettuce.core.RedisClient;
import io.lettuce.core.pubsub.RedisPubSubListener;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.misc.Unsafe;

public class MessagePubsubListener implements RedisPubSubListener<String, byte[]> {

private static Unsafe unsafe;
static {
try {
var field = Unsafe.class.getDeclaredField("theUnsafe");
field.setAccessible(true);
unsafe = (Unsafe) field.get(null);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
}

private static final Logger LOGGER = LoggerFactory.getLogger(MessagePubsubListener.class);
private final MessageHandlerRegistry registry;

Expand All @@ -25,7 +36,6 @@ public void message(String channel, byte[] message) {
}

@Override
@SuppressWarnings("unchecked")
public void message(String pattern, String channel, byte[] message) {
// Strip channel's prefix so we get class name
var className = channel.replace(RedisConfig.PREFIX, "");
Expand All @@ -38,13 +48,11 @@ public void message(String pattern, String channel, byte[] message) {
return;
}

var messagePackSerializable = ((Class<MessagePackSerializable>) clazz)
.getDeclaredConstructor()
.newInstance();
var messagePackSerializable = (MessagePackSerializable) unsafe.allocateInstance(clazz);
messagePackSerializable.deserialize(message);

registry.callAll(clazz, messagePackSerializable);
} catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException | IOException e) {
} catch (ClassNotFoundException | InstantiationException | IOException e) {
LOGGER.error("error while handling message on channel {} with classname {}", channel, className, e);
}
}
Expand Down

0 comments on commit 857b09a

Please sign in to comment.