1616
1717package dev .openfunction .invoker .runtime ;
1818
19- import dev .openfunction .functions .AsyncFunction ;
2019import dev .openfunction .functions .CloudEventFunction ;
2120import dev .openfunction .functions .HttpFunction ;
21+ import dev .openfunction .functions .OpenFunction ;
2222import dev .openfunction .functions .Out ;
2323import dev .openfunction .invoker .context .RuntimeContext ;
2424import dev .openfunction .invoker .context .UserContext ;
@@ -54,7 +54,7 @@ public class SynchronizeRuntime extends HttpServlet implements Runtime {
5454
5555 private CloudEventFunction cloudEventFunction ;
5656
57- private AsyncFunction asyncFunction ;
57+ private OpenFunction openFunction ;
5858
5959 private RuntimeContext runtimeContext ;
6060
@@ -69,17 +69,14 @@ private SynchronizeRuntime(CloudEventFunction cloudEventFunction) {
6969 EventFormatProvider .getInstance ().registerFormat (new JsonEventFormat ());
7070 }
7171
72- private SynchronizeRuntime (AsyncFunction asyncFunction ) {
73- this .asyncFunction = asyncFunction ;
74- daprClient = new DaprClientBuilder ().build ();
75- daprClient .waitForSidecar (WaitDaprSidecarTimeout );
72+ private SynchronizeRuntime (OpenFunction openFunction ) {
73+ this .openFunction = openFunction ;
7674 }
7775
7876 /**
7977 * Makes a {@link SynchronizeRuntime} for the given class.
8078 *
8179 * @param functionClass function class
82- *
8380 * @return {@link SynchronizeRuntime}
8481 */
8582 public static SynchronizeRuntime forClass (Class <?> functionClass ) {
@@ -93,10 +90,10 @@ public static SynchronizeRuntime forClass(Class<?> functionClass) {
9390 Class <? extends CloudEventFunction > cloudEventFunctionClass = functionClass .asSubclass (CloudEventFunction .class );
9491 CloudEventFunction cloudEventFunction = cloudEventFunctionClass .getConstructor ().newInstance ();
9592 return new SynchronizeRuntime (cloudEventFunction );
96- } else if (AsyncFunction .class .isAssignableFrom (functionClass )) {
97- Class <? extends AsyncFunction > asyncFunctionClass = functionClass .asSubclass (AsyncFunction .class );
98- AsyncFunction asyncFunction = asyncFunctionClass .getConstructor ().newInstance ();
99- return new SynchronizeRuntime (asyncFunction );
93+ } else if (OpenFunction .class .isAssignableFrom (functionClass )) {
94+ Class <? extends OpenFunction > asyncFunctionClass = functionClass .asSubclass (OpenFunction .class );
95+ OpenFunction openFunction = asyncFunctionClass .getConstructor ().newInstance ();
96+ return new SynchronizeRuntime (openFunction );
10097 }
10198 } catch (ReflectiveOperationException e ) {
10299 throw new Error ("Could not construct an instance of " + functionClass .getName (), e );
@@ -151,9 +148,9 @@ public void service(HttpServletRequest req, HttpServletResponse res) {
151148 }
152149
153150 respImpl .getOutputStream ().write (userContext .getOut ().getData ().array ());
154- } else if (asyncFunction != null ) {
151+ } else if (openFunction != null ) {
155152 userContext .executePrePlugins ();
156- Out out = asyncFunction .accept (userContext , Arrays .toString (reqImpl .getInputStream ().readAllBytes ()));
153+ Out out = openFunction .accept (userContext , Arrays .toString (reqImpl .getInputStream ().readAllBytes ()));
157154 userContext .setOut (out );
158155 userContext .executePostPlugins ();
159156
@@ -196,6 +193,12 @@ public void service(HttpServletRequest req, HttpServletResponse res) {
196193 public void start (RuntimeContext ctx ) throws Exception {
197194
198195 runtimeContext = ctx ;
196+ // create dapr client when dapr sidecar enabled.
197+ if (System .getenv ("DAPR_GRPC_PORT" ) != null || System .getenv ("DAPR_HTTP_PORT" ) != null ) {
198+ daprClient = new DaprClientBuilder ().build ();
199+ daprClient .waitForSidecar (Runtime .WaitDaprSidecarTimeout );
200+ }
201+
199202 Server server = new Server (ctx .getPort ());
200203
201204 ServletContextHandler servletContextHandler = new ServletContextHandler ();
0 commit comments