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

Major work including a new nio implementation to replacing netty #85

Merged
merged 1 commit into from
May 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,16 @@ public T newInstance() throws DempsyException {
* Invokes the activation method of the passed instance.
*/
@Override
public void activate(final T instance, final Object key, final byte[] activationData) throws IllegalArgumentException, DempsyException {
wrap(() -> activationMethod.invoke(instance, key, activationData));
public void activate(final T instance, final Object key) throws IllegalArgumentException, DempsyException {
wrap(() -> activationMethod.invoke(instance, key));
}

/**
* Invokes the passivation method of the passed instance. Will return the object's passivation data, <code>null</code> if there is none.
*/
@Override
public byte[] passivate(final T instance) throws IllegalArgumentException, DempsyException {
return wrap(() -> (byte[]) passivationMethod.invoke(instance));
public void passivate(final T instance) throws IllegalArgumentException, DempsyException {
wrap(() -> (byte[]) passivationMethod.invoke(instance));
}

/**
Expand Down Expand Up @@ -318,7 +318,6 @@ private Method introspectClone() throws IllegalStateException {
protected class MethodHandle {
private final Method method;
private int keyPosition = -1;
private int binayPosition = -1;
private int totalArguments = 0;

public MethodHandle(final Method method) {
Expand All @@ -332,30 +331,26 @@ public MethodHandle(final Method method, final Class<?> keyClass) {
this.totalArguments = parameterTypes.length;
for (int i = 0; i < parameterTypes.length; i++) {
final Class<?> parameter = parameterTypes[i];
if (parameter.isArray() && parameter.getComponentType().isAssignableFrom(byte.class)) {
this.binayPosition = i;
} else if (keyClass != null && parameter.isAssignableFrom(keyClass)) {
if (keyClass != null && parameter.isAssignableFrom(keyClass)) {
this.keyPosition = i;
}
}
}
}

public Object invoke(final Object instance, final Object key, final byte[] data)
public Object invoke(final Object instance, final Object key)
throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
if (this.method != null) {
final Object[] parameters = new Object[this.totalArguments];
if (this.keyPosition > -1)
parameters[this.keyPosition] = key;
if (this.binayPosition > -1)
parameters[this.binayPosition] = data;
return this.method.invoke(instance, parameters);
}
return null;
}

public Object invoke(final Object instance) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
return this.invoke(instance, null, null);
return this.invoke(instance, null);
}

public Method getMethod() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import java.util.function.Supplier;

import net.dempsy.config.ClusterId;
import net.dempsy.messages.KeyedMessageWithType;
import net.dempsy.messages.KeyedMessage;
import net.dempsy.messages.KeyedMessageWithType;
import net.dempsy.messages.MessageProcessorLifecycle;

public class MessageProcessor implements MessageProcessorLifecycle<Mp> {
Expand Down Expand Up @@ -40,13 +40,13 @@ public Mp newInstance() {
}

@Override
public void activate(final Mp instance, final Object key, final byte[] activationData) throws IllegalArgumentException {
instance.activate(activationData, key);
public void activate(final Mp instance, final Object key) throws IllegalArgumentException {
instance.activate(key);
}

@Override
public byte[] passivate(final Mp instance) throws IllegalArgumentException {
return instance.passivate();
public void passivate(final Mp instance) throws IllegalArgumentException {
instance.passivate();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ public default KeyedMessageWithType[] output() {
return null;
}

public default byte[] passivate() {
return null;
}
public default void passivate() {}

public default void activate(final byte[] data, final Object key) {}
public default void activate(final Object key) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public interface MessageProcessorLifecycle<T> {
/**
* Invokes the activation method of the passed instance.
*/
public void activate(T instance, Object key, byte[] activationData) throws IllegalArgumentException, DempsyException;
public void activate(T instance, Object key) throws IllegalArgumentException, DempsyException;

/**
* Invokes the passivation method of the passed instance. Will return the object's passivation data,
Expand All @@ -27,7 +27,7 @@ public interface MessageProcessorLifecycle<T> {
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
public byte[] passivate(T instance) throws IllegalArgumentException, DempsyException;
public void passivate(T instance) throws IllegalArgumentException, DempsyException;

/**
* Invokes the appropriate message handler of the passed instance. Caller is responsible for not passing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@

package net.dempsy.lifecycle.annotations;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import org.junit.Test;
Expand All @@ -40,12 +38,11 @@ public void testMethodHandleWithParameters() throws Throwable {
helper.validate();
final TestMp mp = helper.newInstance();
assertFalse(mp.isActivated());
helper.activate(mp, "activate", null);
helper.activate(mp, "activate");
assertTrue(mp.isActivated());
assertFalse(mp.ispassivateCalled());
final String ret = new String(helper.passivate(mp));
helper.passivate(mp);
assertTrue(mp.ispassivateCalled());
assertEquals("passivate", ret);
}

@Test
Expand All @@ -54,12 +51,11 @@ public void testMethodHandleWithNoParameters() throws Throwable {
helper.validate();
final TestMpEmptyActivate mp = helper.newInstance();
assertFalse(mp.isActivated());
helper.activate(mp, "activate", null);
helper.activate(mp, "activate");
assertTrue(mp.isActivated());
assertFalse(mp.ispassivateCalled());
final Object ret = helper.passivate(mp);
helper.passivate(mp);
assertTrue(mp.ispassivateCalled());
assertNull(ret);
}

@Test
Expand All @@ -68,12 +64,11 @@ public void testMethodHandleWithOnlyKey() throws Throwable {
helper.validate();
final TestMpOnlyKey mp = helper.newInstance();
assertFalse(mp.isActivated());
helper.activate(mp, "activate", null);
helper.activate(mp, "activate");
assertTrue(mp.isActivated());
assertFalse(mp.ispassivateCalled());
final String ret = new String(helper.passivate(mp));
helper.passivate(mp);
assertTrue(mp.ispassivateCalled());
assertEquals("passivate", ret);
}

@Test
Expand All @@ -82,12 +77,11 @@ public void testMethodHandleExtraParameters() throws Throwable {
helper.validate();
final TestMpExtraParameters mp = helper.newInstance();
assertFalse(mp.isActivated());
helper.activate(mp, "activate", null);
helper.activate(mp, "activate");
assertTrue(mp.isActivated());
assertFalse(mp.ispassivateCalled());
final String ret = new String(helper.passivate(mp));
helper.passivate(mp);
assertTrue(mp.ispassivateCalled());
assertEquals("passivate", ret);
}

@Test
Expand All @@ -97,12 +91,11 @@ public void testMethodHandleExtraParametersOrderChanged() throws Throwable {
helper.validate();
final TestMpExtraParametersChangedOrder mp = helper.newInstance();
assertFalse(mp.isActivated());
helper.activate(mp, "activate", null);
helper.activate(mp, "activate");
assertTrue(mp.isActivated());
assertFalse(mp.ispassivateCalled());
final Object ret = helper.passivate(mp);
helper.passivate(mp);
assertTrue(mp.ispassivateCalled());
assertNull(ret);
}

@Test
Expand All @@ -111,25 +104,23 @@ public void testMethodHandleNoActivation() throws Throwable {
helper.validate();
final TestMpNoActivation mp = helper.newInstance();
assertFalse(mp.isActivated());
helper.activate(mp, "activate", null);
helper.activate(mp, "activate");
assertFalse(mp.isActivated());
assertFalse(mp.ispassivateCalled());
final Object ret = helper.passivate(mp);
helper.passivate(mp);
assertFalse(mp.ispassivateCalled());
assertNull(ret);
}

@Test
public void testMethodHandleNoKey() throws Throwable {
final MessageProcessor<TestMpNoKey> helper = new MessageProcessor<TestMpNoKey>(new TestMpNoKey());
final TestMpNoKey mp = helper.newInstance();
assertFalse(mp.isActivated());
helper.activate(mp, "activate", null);
helper.activate(mp, "activate");
assertFalse(mp.isActivated());
assertFalse(mp.ispassivateCalled());
final Object ret = helper.passivate(mp);
helper.passivate(mp);
assertFalse(mp.ispassivateCalled());
assertNull(ret);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ public int hashCode() {
@Mp
public static class InvocationTestMp implements Cloneable {
public boolean isActivated;
public String activationValue;
public boolean isPassivated;
public String lastStringHandlerValue;
public Number lastNumberHandlerValue;
Expand All @@ -150,15 +149,13 @@ public InvocationTestMp clone() throws CloneNotSupportedException {
}

@Activation
public void activate(final byte[] data) {
public void activate() {
isActivated = true;
activationValue = new String(data);
}

@Passivation
public byte[] passivate() {
public void passivate() {
isPassivated = true;
return activationValue.getBytes();
}

@MessageHandler
Expand Down Expand Up @@ -246,14 +243,12 @@ public void testLifeCycleMethods() {
assertNotSame("instantiation failed; returned prototype", prototype, instance);

assertFalse("instance activated before activation method called", instance.isActivated);
invoker.activate(instance, null, "ABC".getBytes());
invoker.activate(instance, null);
assertTrue("instance was not activated", instance.isActivated);
assertEquals("ABC", instance.activationValue);

assertFalse("instance passivated before passivation method called", instance.isPassivated);
final byte[] data = invoker.passivate(instance);
invoker.passivate(instance);
assertTrue("instance was not passivated", instance.isPassivated);
assertEquals("ABC", new String(data));
}

@Test(expected = IllegalStateException.class)
Expand Down
Loading