This example provides a Function to consume a Notification. The function accepts a typed message event.
- Notifications appends metadata to the headers Standard header metadata
- The Notification message body is a user specified Schema or String. Note: This library currently supports JSON and String types.
- Limits for Notifications Notification limits
- fn-events for NotificationFunction classes.
- fn-events-testing for NotificationFunction testing library.
This example showcases how to use the fn-event NotificationFunction to use a Function to consume messages from a Notification topic.
Set up the Notification Topic with Function Subscription:
The Function entrypoint extends the NotificationFunction abstract class.
Note: the func.yaml entrypoint remains the class which extends NotificationFunction
e.g. cmd: com.fnproject.fn.examples.Function::handler
package com.fnproject.fn.examples;
import com.fnproject.events.NotificationFunction;
import com.fnproject.events.input.NotificationMessage;
public class Function extends NotificationFunction<Employee> {
public NotificationService notificationService;
public Function() {
this.notificationService = new NotificationService();
}
@Override
public void handler(NotificationMessage<Employee> content) {
notificationService.readNotification(content);
}
}The NotificationMessage.java
content contains the message body.
The class Employee.java is the schema for the message content.
Function.java
public class Function extends NotificationFunction<Employee>.
To return an error response, throw RuntimeException.class. Doing so will cause the Function to return a 502 Retry policy
Unit testing NotificationFunction is supported with the NotificationTestFeature and FnTestingRule.
First of all, the class initializes the FnTestingRule harness, as explained
in Testing Functions.
package com.fnproject.fn.examples;
import static org.junit.Assert.assertEquals;
import com.fnproject.events.input.NotificationMessage;
import com.fnproject.events.testing.NotificationTestFeature;
import com.fnproject.fn.api.Headers;
import com.fnproject.fn.testing.FnResult;
import com.fnproject.fn.testing.FnTestingRule;
import org.junit.Rule;
import org.junit.Test;
public class FunctionTest {
@Rule
public FnTestingRule fn = FnTestingRule.createDefault();
private final NotificationTestFeature connectorHubTestFeature = NotificationTestFeature.createDefault(fn);
@Test
public void testInvokeFunctionWithLoggingData() throws Exception {
NotificationMessage<Employee> event = createMinimalRequest();
connectorHubTestFeature.givenEvent(event).enqueue();
fn.thenRun(Function.class, "handler");
FnResult result = fn.getOnlyResult();
assertEquals(200, result.getStatus().getCode());
}
private static NotificationMessage createMinimalRequest() {
Employee employee = new Employee();
employee.setName("foo");
return new NotificationMessage<>(employee, Headers.emptyHeaders());
}
}Use connectorHubTestFeature.givenEvent(event).enqueue(); to queue the request event
and invoke the Function with fn.thenRun(Function.class, "handler");.
To verify the Subscription is working, check Notification metrics