mirror of https://github.com/dapr/java-sdk.git
Moving io.dapr.runtime to test + fixing examples accordingly. (#121)
This commit is contained in:
parent
04ee301371
commit
00251b7e31
|
@ -0,0 +1,25 @@
|
|||
/*
|
||||
* Copyright (c) Microsoft Corporation.
|
||||
* Licensed under the MIT License.
|
||||
*/
|
||||
|
||||
package io.dapr.examples.bindings.http;
|
||||
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
/**
|
||||
* SpringBoot Controller to handle input binding.
|
||||
*/
|
||||
@RestController
|
||||
public class InputBindingController {
|
||||
|
||||
@PostMapping(path = "/sample123")
|
||||
public Mono<Void> handleInputBinding(@RequestBody(required = false) byte[] body) {
|
||||
return Mono.fromRunnable(() ->
|
||||
System.out.println("Received message through binding: " + (body == null ? "" : new String(body))));
|
||||
}
|
||||
|
||||
}
|
|
@ -5,14 +5,11 @@
|
|||
|
||||
package io.dapr.examples.bindings.http;
|
||||
|
||||
import io.dapr.runtime.Dapr;
|
||||
import io.dapr.springboot.DaprApplication;
|
||||
import org.apache.commons.cli.CommandLine;
|
||||
import org.apache.commons.cli.CommandLineParser;
|
||||
import org.apache.commons.cli.DefaultParser;
|
||||
import org.apache.commons.cli.Options;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
/**
|
||||
* Service for input binding example.
|
||||
|
@ -22,7 +19,6 @@ import reactor.core.publisher.Mono;
|
|||
* 3. Run :
|
||||
* dapr run --app-id inputbinding --app-port 3000 --port 3005 -- mvn exec:java -Dexec.mainClass=io.dapr.examples.bindings.http.InputBindingExample -Dexec.args="-p 3000"
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class InputBindingExample {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
@ -35,16 +31,6 @@ public class InputBindingExample {
|
|||
// If port string is not valid, it will throw an exception.
|
||||
int port = Integer.parseInt(cmd.getOptionValue("port"));
|
||||
|
||||
final String BINDING_NAME = "sample123";
|
||||
|
||||
// "sample123" is the name of the binding. It will be received at url /v1.0/bindings/sample123
|
||||
Dapr.getInstance().registerInputBinding(BINDING_NAME, (message, metadata) -> Mono
|
||||
.fromSupplier(() -> {
|
||||
System.out.println("Received message through binding: " + (message == null ? "" : new String(message)));
|
||||
return Boolean.TRUE;
|
||||
})
|
||||
.then(Mono.empty()));
|
||||
|
||||
// Start Dapr's callback endpoint.
|
||||
DaprApplication.start(port);
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@ import io.dapr.utils.ObjectSerializer;
|
|||
* mvn clean install
|
||||
* 2. cd to [repo-root]/examples
|
||||
* 3. Run the program:
|
||||
* dapr run --app-id outputbinding --port 3006 -- mvn exec:java -pl=examples -Dexec.mainClass=io.dapr.examples.bindings.http.OutputBindingExample
|
||||
* dapr run --app-id outputbinding --port 3006 -- mvn exec:java -Dexec.mainClass=io.dapr.examples.bindings.http.OutputBindingExample
|
||||
*/
|
||||
public class OutputBindingExample {
|
||||
|
||||
|
|
|
@ -5,33 +5,11 @@
|
|||
|
||||
package io.dapr.examples.invoke.http;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.google.protobuf.Any;
|
||||
import com.google.protobuf.InvalidProtocolBufferException;
|
||||
import io.dapr.DaprClientGrpc;
|
||||
import io.dapr.DaprClientProtos;
|
||||
import io.dapr.runtime.Dapr;
|
||||
import io.dapr.runtime.MethodListener;
|
||||
import io.dapr.springboot.DaprApplication;
|
||||
import io.grpc.Server;
|
||||
import io.grpc.ServerBuilder;
|
||||
import io.grpc.stub.StreamObserver;
|
||||
import org.apache.commons.cli.CommandLine;
|
||||
import org.apache.commons.cli.CommandLineParser;
|
||||
import org.apache.commons.cli.DefaultParser;
|
||||
import org.apache.commons.cli.Options;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Map;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import static io.dapr.examples.DaprExamplesProtos.SayRequest;
|
||||
import static io.dapr.examples.DaprExamplesProtos.SayResponse;
|
||||
|
||||
/**
|
||||
* 1. Build and install jars:
|
||||
|
@ -41,16 +19,6 @@ import static io.dapr.examples.DaprExamplesProtos.SayResponse;
|
|||
*/
|
||||
public class DemoService {
|
||||
|
||||
/**
|
||||
* Shared Json serializer/deserializer.
|
||||
*/
|
||||
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
||||
|
||||
/**
|
||||
* Format to output date and time.
|
||||
*/
|
||||
private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
|
||||
|
||||
/**
|
||||
* Starts the service.
|
||||
* @param args Expects the port: -p PORT
|
||||
|
@ -66,21 +34,6 @@ public class DemoService {
|
|||
// If port string is not valid, it will throw an exception.
|
||||
int port = Integer.parseInt(cmd.getOptionValue("port"));
|
||||
|
||||
Dapr.getInstance().registerServiceMethod("say", (data, metadata) -> {
|
||||
String message = data == null ? "" : new String(data, StandardCharsets.UTF_8);
|
||||
|
||||
Calendar utcNow = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
|
||||
String utcNowAsString = DATE_FORMAT.format(utcNow.getTime());
|
||||
|
||||
String metadataString = metadata == null ? "" : OBJECT_MAPPER.writeValueAsString(metadata);
|
||||
|
||||
// Handles the request by printing message.
|
||||
System.out.println(
|
||||
"Server: " + message + " @ " + utcNowAsString + " and metadata: " + metadataString);
|
||||
|
||||
return Mono.just(utcNowAsString.getBytes(StandardCharsets.UTF_8));
|
||||
});
|
||||
|
||||
DaprApplication.start(port);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,61 @@
|
|||
/*
|
||||
* Copyright (c) Microsoft Corporation.
|
||||
* Licensed under the MIT License.
|
||||
*/
|
||||
|
||||
package io.dapr.examples.invoke.http;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestHeader;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Map;
|
||||
import java.util.TimeZone;
|
||||
|
||||
/**
|
||||
* SpringBoot Controller to handle input binding.
|
||||
*/
|
||||
@RestController
|
||||
public class DemoServiceController {
|
||||
|
||||
/**
|
||||
* Json serializer/deserializer.
|
||||
*/
|
||||
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
||||
|
||||
/**
|
||||
* Format to output date and time.
|
||||
*/
|
||||
private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
|
||||
|
||||
@PostMapping(path = "/say")
|
||||
public Mono<String> handleMethod(@RequestBody(required = false) byte[] body,
|
||||
@RequestHeader Map<String, String> headers) {
|
||||
return Mono.fromSupplier(() -> {
|
||||
try {
|
||||
String message = body == null ? "" : new String(body, StandardCharsets.UTF_8);
|
||||
|
||||
Calendar utcNow = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
|
||||
String utcNowAsString = DATE_FORMAT.format(utcNow.getTime());
|
||||
|
||||
String metadataString = headers == null ? "" : OBJECT_MAPPER.writeValueAsString(headers);
|
||||
|
||||
// Handles the request by printing message.
|
||||
System.out.println(
|
||||
"Server: " + message + " @ " + utcNowAsString + " and metadata: " + metadataString);
|
||||
|
||||
return utcNowAsString;
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
|
@ -5,14 +5,12 @@
|
|||
|
||||
package io.dapr.examples.pubsub.http;
|
||||
|
||||
import io.dapr.runtime.Dapr;
|
||||
import io.dapr.springboot.DaprApplication;
|
||||
import org.apache.commons.cli.CommandLine;
|
||||
import org.apache.commons.cli.CommandLineParser;
|
||||
import org.apache.commons.cli.DefaultParser;
|
||||
import org.apache.commons.cli.Options;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
/**
|
||||
* Service for subscriber.
|
||||
|
@ -34,14 +32,6 @@ public class Subscriber {
|
|||
// If port string is not valid, it will throw an exception.
|
||||
int port = Integer.parseInt(cmd.getOptionValue("port"));
|
||||
|
||||
// Subscribe to topic.
|
||||
Dapr.getInstance().subscribeToTopic("message", (envelope, metadata) -> Mono
|
||||
.fromSupplier(() -> {
|
||||
System.out.println("Subscriber got message: " + (envelope.getData() == null ? "" : new String(envelope.getData())));
|
||||
return Boolean.TRUE;
|
||||
})
|
||||
.then(Mono.empty()));
|
||||
|
||||
// Start Dapr's callback endpoint.
|
||||
DaprApplication.start(port);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
/*
|
||||
* Copyright (c) Microsoft Corporation.
|
||||
* Licensed under the MIT License.
|
||||
*/
|
||||
|
||||
package io.dapr.examples.pubsub.http;
|
||||
|
||||
import io.dapr.client.domain.CloudEventEnvelope;
|
||||
import io.dapr.utils.ObjectSerializer;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* SpringBoot Controller to handle input binding.
|
||||
*/
|
||||
@RestController
|
||||
public class SubscriberController {
|
||||
|
||||
/**
|
||||
* Dapr's default serializer/deserializer.
|
||||
*/
|
||||
private static final ObjectSerializer SERIALIZER = new ObjectSerializer();
|
||||
|
||||
@GetMapping("/dapr/subscribe")
|
||||
public byte[] daprConfig() throws Exception {
|
||||
return SERIALIZER.serialize(new String[] { "message" });
|
||||
}
|
||||
|
||||
@PostMapping(path = "/message")
|
||||
public Mono<Void> handleMessage(@RequestBody(required = false) byte[] body,
|
||||
@RequestHeader Map<String, String> headers) {
|
||||
return Mono.fromRunnable(() -> {
|
||||
try {
|
||||
// Dapr's event is compliant to CloudEvent.
|
||||
CloudEventEnvelope envelope = SERIALIZER.deserialize(body, CloudEventEnvelope.class);
|
||||
|
||||
String message = envelope.getData() == null ? "" : new String(envelope.getData());
|
||||
System.out.println("Subscriber got message: " + message);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
|
@ -11,7 +11,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|||
/**
|
||||
* Dapr's HTTP callback implementation via SpringBoot.
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@SpringBootApplication(scanBasePackages = {"io.dapr.springboot", "io.dapr.examples"})
|
||||
public class DaprApplication {
|
||||
|
||||
/**
|
||||
|
|
|
@ -5,19 +5,23 @@
|
|||
|
||||
package io.dapr.springboot;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.dapr.actors.runtime.ActorRuntime;
|
||||
import io.dapr.runtime.Dapr;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* SpringBoot Controller to handle callback APIs for Dapr.
|
||||
*/
|
||||
@RestController
|
||||
public class DaprController {
|
||||
|
||||
@Autowired
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
private String topics;
|
||||
|
||||
@GetMapping("/")
|
||||
public String index() {
|
||||
return "Greetings from Dapr!";
|
||||
|
@ -28,21 +32,6 @@ public class DaprController {
|
|||
return ActorRuntime.getInstance().serializeConfig();
|
||||
}
|
||||
|
||||
@GetMapping("/dapr/subscribe")
|
||||
public String daprSubscribe() throws Exception {
|
||||
return Dapr.getInstance().serializeSubscribedTopicList();
|
||||
}
|
||||
|
||||
@PostMapping(path = "/{name}")
|
||||
@PutMapping(path = "/{name}")
|
||||
@DeleteMapping(path = "/{name}")
|
||||
@GetMapping(path = "/{name}")
|
||||
public Mono<byte[]> invokeMethodOrTopic(@PathVariable("name") String name,
|
||||
@RequestBody(required = false) byte[] body,
|
||||
@RequestHeader Map<String, String> header) {
|
||||
return Dapr.getInstance().handleInvocation(name, body, header);
|
||||
}
|
||||
|
||||
@PostMapping(path = "/actors/{type}/{id}")
|
||||
public Mono<Void> activateActor(@PathVariable("type") String type,
|
||||
@PathVariable("id") String id) throws Exception {
|
||||
|
|
|
@ -13,6 +13,8 @@ import java.util.Map;
|
|||
|
||||
/**
|
||||
* Common interface to configure and process callback API.
|
||||
*
|
||||
* Used for Integration Tests only, for now.
|
||||
*/
|
||||
public interface DaprRuntime {
|
||||
|
Loading…
Reference in New Issue