Skip to content

Commit

Permalink
go and java
Browse files Browse the repository at this point in the history
Signed-off-by: Hannah Hunter <[email protected]>
  • Loading branch information
hhunter-ms committed Aug 1, 2023
1 parent 5ccf050 commit 6b2b49a
Showing 1 changed file with 37 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -262,33 +262,33 @@ namespace EventService

```java
//dependencies
package com.service;

import org.json.JSONObject;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Random;
import java.util.concurrent.TimeUnit;

//code
@SpringBootApplication

public class CheckoutServiceApplication {
private static final HttpClient httpClient = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_2)
.connectTimeout(Duration.ofSeconds(10))
.build();

private static final String DAPR_HTTP_PORT = System.getenv().getOrDefault("DAPR_HTTP_PORT", "3500");

public static void main(String[] args) throws InterruptedException, IOException {
String dapr_url = "http://localhost:" + DAPR_HTTP_PORT + "/orders";
for (int i=1; i<=20; i++) {
int orderId = i;
JSONObject obj = new JSONObject();
obj.put("orderId", orderId);

public static void main(String[] args) throws InterruptedException{
while(true) {
TimeUnit.MILLISECONDS.sleep(5000);
Random random = new Random();
int orderId = random.nextInt(1000-1) + 1;
HttpRequest request = HttpRequest.newBuilder()
.POST(HttpRequest.BodyPublishers.ofString(obj.toString()))
.uri(URI.create(dapr_url))
Expand All @@ -299,6 +299,9 @@ public class CheckoutServiceApplication {
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println("Order passed: "+ orderId);
TimeUnit.MILLISECONDS.sleep(1000);

log.info("Order requested: " + orderId);
log.info("Result: " + result);
}
}
}
Expand All @@ -309,56 +312,51 @@ public class CheckoutServiceApplication {
{{% codetab %}}

```go
//dependencies
package main

//dependencies
import (
"fmt"
"io/ioutil"
"context"
"log"
"net/http"
"os"
"strconv"
"strings"
"math/rand"
"time"
"strconv"
)

//code
type Order struct {
orderName string
orderNum string
}

func main() {
daprHost := os.Getenv("DAPR_HOST")
if daprHost == "" {
daprHost = "http://localhost"
}
daprHttpPort := os.Getenv("DAPR_HTTP_PORT")
if daprHttpPort == "" {
daprHttpPort = "3500"
}
client := &http.Client{
Timeout: 15 * time.Second,
}
for i := 1; i <= 20; i++ {
order := `{"orderId":` + strconv.Itoa(i) + "}"
req, err := http.NewRequest("POST", daprHost+":"+daprHttpPort+"/orders", strings.NewReader(order))

for i := 0; i < 10; i++ {
time.Sleep(5000)
orderId := rand.Intn(1000-1) + 1
client, err := dapr.NewClient()
if err != nil {
log.Fatal(err.Error())
panic(err)
}

// Adding app id as part of the header
// Adding app id as part of the header
req.Header.Add("dapr-app-id", "order-processor")

// Invoking a service
response, err := client.Do(req)
if err != nil {
log.Fatal(err.Error())
}

// Read the response
result, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
response.Body.Close()

fmt.Println("Order passed:", string(result))
result, err := client.InvokeMethod(ctx, "checkout", "checkout/" + strconv.Itoa(orderId), "get")
log.Println("Order requested: " + strconv.Itoa(orderId))
log.Println("Result: ")
log.Println(result)
}
}
```
Expand Down

0 comments on commit 6b2b49a

Please sign in to comment.