Skip to content

Commit

Permalink
feat: add context to Java runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
alexec committed Jun 10, 2021
1 parent 5becca6 commit e61c307
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 13 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,15 @@ ADD runtimes/go1-16 /workspace
RUN chown -R 9653 /.cache /workspace
WORKDIR /workspace
USER 9653:9653
RUN go build ./...
ENTRYPOINT ./entrypoint.sh

FROM openjdk:16 AS java16
ADD runtimes/java16 /workspace
RUN chown -R 9653 /workspace
WORKDIR /workspace
USER 9653:9653
RUN javac *.java
ENTRYPOINT ./entrypoint.sh

FROM python:3.9-alpine AS python3-9
Expand Down
12 changes: 6 additions & 6 deletions dsls/python/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


def str_presenter(dumper, data):
if len(data.splitlines()) > 1 or '"' in data or "'" in data:
if '\n' in data or '"' in data or "'" in data:
return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='|')
return dumper.represent_scalar('tag:yaml.org,2002:str', data)

Expand Down Expand Up @@ -433,7 +433,7 @@ def dump(self):


class KafkaSource(Source):
def __init__(self, topic, name=None, retryPolicy=None):
def __init__(self, topic, name=None, retryPolicy=None):
super().__init__(name=name, retryPolicy=retryPolicy)
self._topic = topic

Expand All @@ -444,7 +444,7 @@ def dump(self):


class STANSource(Source):
def __init__(self, subject, name=None, retryPolicy=None):
def __init__(self, subject, name=None, retryPolicy=None):
super().__init__(name=name, retryPolicy=retryPolicy)
self._subject = subject

Expand All @@ -463,9 +463,9 @@ def http(name=None, retryPolicy=None):
return HTTPSource(name=name, retryPolicy=retryPolicy)


def kafka(topic, name=None, retryPolicy=None):
return KafkaSource(topic, name=name, retryPolicy=retryPolicy)
def kafka(topic, name=None, retryPolicy=None):
return KafkaSource(topic, name=name, retryPolicy=retryPolicy)


def stan(subject, name=None, retryPolicy=None):
def stan(subject, name=None, retryPolicy=None):
return STANSource(subject, name=name, retryPolicy=retryPolicy)
5 changes: 3 additions & 2 deletions examples/104-java16-pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ def handler(msg):
[Learn about handlers](../docs/HANDLERS.md)""")
.step(
(kafka('input-topic')
.handler('main', code="""public class Handler {
public static byte[] Handle(byte[] msg) throws Exception {
.handler('main', code="""import java.util.Map;
public static byte[] Handle(byte[] msg, Map<String,String> context) throws Exception {
return msg;
}
}""", runtime='java16')
Expand Down
5 changes: 3 additions & 2 deletions examples/104-java16-pipeline.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ spec:
steps:
- handler:
code: |-
public class Handler {
public static byte[] Handle(byte[] msg) throws Exception {
import java.util.Map;
public static byte[] Handle(byte[] msg, Map<String,String> context) throws Exception {
return msg;
}
}
Expand Down
4 changes: 3 additions & 1 deletion runtimes/java16/Handler.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import java.util.Map;

public class Handler {
public static byte[] Handle(byte[] msg) throws Exception {
public static byte[] Handle(byte[] msg, Map<String,String> context) throws Exception {
var x = "hi " + new String(msg);
return x.getBytes();
}
Expand Down
4 changes: 2 additions & 2 deletions runtimes/java16/Main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import com.sun.net.httpserver.HttpServer;

import java.io.ByteArrayOutputStream;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
Expand All @@ -9,6 +8,7 @@
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.Executors;
import java.util.Collections;

public class Main {
public static void main(String[] args) throws Exception {
Expand All @@ -25,7 +25,7 @@ public static void main(String[] args) throws Exception {
}
isr.close();
try {
var out = Handler.Handle(in.toByteArray());
var out = Handler.Handle(in.toByteArray(), Collections.<String,String>emptyMap());
if (out != null) {
he.sendResponseHeaders(201, 0);
try (var os = he.getResponseBody()) {
Expand Down

0 comments on commit e61c307

Please sign in to comment.