use of org.apache.pulsar.functions.api.Function in project incubator-pulsar by apache.
the class JavaInstanceRunnableTest method testExplicitDefaultSerDe.
/**
* Verify that Explicit setting of Default Serializer works fine.
*/
@Test
public void testExplicitDefaultSerDe() {
try {
JavaInstanceRunnable runnable = createRunnable(false, DefaultSerDe.class.getName());
Method method = makeAccessible(runnable);
ClassLoader clsLoader = Thread.currentThread().getContextClassLoader();
Function function = (Function<String, String>) (input, context) -> input + "-lambda";
Class<?>[] typeArgs = TypeResolver.resolveRawArguments(Function.class, function.getClass());
method.invoke(runnable, typeArgs, clsLoader);
} catch (Exception ex) {
assertTrue(false);
}
}
use of org.apache.pulsar.functions.api.Function in project incubator-pulsar by apache.
the class JavaInstanceRunnableTest method testDefaultSerDe.
/**
* Verify that Default Serializer works fine.
*/
@Test
public void testDefaultSerDe() {
try {
JavaInstanceRunnable runnable = createRunnable(false, null);
Method method = makeAccessible(runnable);
ClassLoader clsLoader = Thread.currentThread().getContextClassLoader();
Function function = (Function<String, String>) (input, context) -> input + "-lambda";
Class<?>[] typeArgs = TypeResolver.resolveRawArguments(Function.class, function.getClass());
method.invoke(runnable, typeArgs, clsLoader);
} catch (Exception ex) {
ex.printStackTrace();
assertEquals(ex, null);
assertTrue(false);
}
}
use of org.apache.pulsar.functions.api.Function in project incubator-pulsar by apache.
the class JavaInstanceRunnableTest method testInconsistentOutputType.
/**
* Verify that function output type should be consistent with output serde type.
*/
@Test
public void testInconsistentOutputType() {
try {
JavaInstanceRunnable runnable = createRunnable(false, IntegerSerDe.class.getName());
Method method = makeAccessible(runnable);
ClassLoader clsLoader = Thread.currentThread().getContextClassLoader();
Function function = (Function<String, String>) (input, context) -> input + "-lambda";
Class<?>[] typeArgs = TypeResolver.resolveRawArguments(Function.class, function.getClass());
method.invoke(runnable, typeArgs, clsLoader);
fail("Should fail constructing java instance if function type is inconsistent with serde type");
} catch (InvocationTargetException ex) {
assertTrue(ex.getCause().getMessage().startsWith("Inconsistent types found between function output type and output serde type:"));
} catch (Exception ex) {
assertTrue(false);
}
}
use of org.apache.pulsar.functions.api.Function in project incubator-pulsar by apache.
the class JavaInstanceRunnableTest method testInconsistentInputType.
/**
* Verify that function input type should be consistent with input serde type.
*/
@Test
public void testInconsistentInputType() {
try {
JavaInstanceRunnable runnable = createRunnable(true, DefaultSerDe.class.getName());
Method method = makeAccessible(runnable);
ClassLoader clsLoader = Thread.currentThread().getContextClassLoader();
Function function = (Function<String, String>) (input, context) -> input + "-lambda";
Class<?>[] typeArgs = TypeResolver.resolveRawArguments(Function.class, function.getClass());
method.invoke(runnable, typeArgs, clsLoader);
fail("Should fail constructing java instance if function type is inconsistent with serde type");
} catch (InvocationTargetException ex) {
assertTrue(ex.getCause().getMessage().startsWith("Inconsistent types found between function input type and input serde type:"));
} catch (Exception ex) {
assertTrue(false);
}
}
use of org.apache.pulsar.functions.api.Function in project incubator-pulsar by apache.
the class JavaInstanceRunnable method setupJavaInstance.
/**
* NOTE: this method should be called in the instance thread, in order to make class loading work.
*/
JavaInstance setupJavaInstance() throws Exception {
// initialize the thread context
ThreadContext.put("function", FunctionConfigUtils.getFullyQualifiedName(instanceConfig.getFunctionConfig()));
ThreadContext.put("instance", instanceConfig.getInstanceId());
log.info("Starting Java Instance {}", instanceConfig.getFunctionConfig().getName());
// start the function thread
loadJars();
ClassLoader clsLoader = Thread.currentThread().getContextClassLoader();
Object object = Reflections.createInstance(instanceConfig.getFunctionConfig().getClassName(), clsLoader);
if (!(object instanceof Function) && !(object instanceof java.util.function.Function)) {
throw new RuntimeException("User class must either be Function or java.util.Function");
}
Class<?>[] typeArgs;
if (object instanceof Function) {
Function function = (Function) object;
typeArgs = TypeResolver.resolveRawArguments(Function.class, function.getClass());
} else {
java.util.function.Function function = (java.util.function.Function) object;
typeArgs = TypeResolver.resolveRawArguments(java.util.function.Function.class, function.getClass());
}
// setup serde
setupSerDe(typeArgs, clsLoader);
// start the state table
setupStateTable();
// start the output producer
startOutputProducer();
// start the input consumer
startInputConsumer();
// start any log topic handler
setupLogHandler();
return new JavaInstance(instanceConfig, object, clsLoader, client, inputConsumers);
}
Aggregations