mirror of https://github.com/dapr/quickstarts.git
Added ack messages once received in pubsub
This commit is contained in:
parent
13b29cb07f
commit
90b15a3268
|
|
@ -2,6 +2,8 @@ using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System;
|
using System;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Net;
|
||||||
using Dapr;
|
using Dapr;
|
||||||
using Dapr.Client;
|
using Dapr.Client;
|
||||||
|
|
||||||
|
|
@ -12,9 +14,10 @@ namespace CheckoutService.controller
|
||||||
{
|
{
|
||||||
[Topic("order_pub_sub", "orders")]
|
[Topic("order_pub_sub", "orders")]
|
||||||
[HttpPost("checkout")]
|
[HttpPost("checkout")]
|
||||||
public void getCheckout([FromBody] int orderId)
|
public HttpResponseMessage getCheckout([FromBody] int orderId)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Subscriber received : " + orderId);
|
Console.WriteLine("Subscriber received : " + orderId);
|
||||||
|
return new HttpResponseMessage(HttpStatusCode.OK);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,9 +1,10 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"context"
|
|
||||||
|
|
||||||
"github.com/dapr/go-sdk/service/common"
|
"github.com/dapr/go-sdk/service/common"
|
||||||
daprd "github.com/dapr/go-sdk/service/http"
|
daprd "github.com/dapr/go-sdk/service/http"
|
||||||
|
|
@ -17,6 +18,7 @@ var sub = &common.Subscription{
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s := daprd.NewService(":6002")
|
s := daprd.NewService(":6002")
|
||||||
|
http.HandleFunc("/checkout", handleRequest)
|
||||||
if err := s.AddTopicEventHandler(sub, eventHandler); err != nil {
|
if err := s.AddTopicEventHandler(sub, eventHandler); err != nil {
|
||||||
log.Fatalf("error adding topic subscription: %v", err)
|
log.Fatalf("error adding topic subscription: %v", err)
|
||||||
}
|
}
|
||||||
|
|
@ -29,3 +31,16 @@ func eventHandler(ctx context.Context, e *common.TopicEvent) (retry bool, err er
|
||||||
log.Printf("Subscriber received: %s", e.Data)
|
log.Printf("Subscriber received: %s", e.Data)
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func handleRequest(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.WriteHeader(200)
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
resp := make(map[string]string)
|
||||||
|
resp["message"] = "Status Ok"
|
||||||
|
jsonResp, err := json.Marshal(resp)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Error happened in JSON marshal. Err: %s", err)
|
||||||
|
}
|
||||||
|
w.Write(jsonResp)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,8 @@ package com.service.CheckoutService.controller;
|
||||||
import io.dapr.Topic;
|
import io.dapr.Topic;
|
||||||
import io.dapr.client.domain.CloudEvent;
|
import io.dapr.client.domain.CloudEvent;
|
||||||
|
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
|
|
@ -18,10 +20,12 @@ public class CheckoutServiceController {
|
||||||
|
|
||||||
@Topic(name = "orders", pubsubName = "order_pub_sub")
|
@Topic(name = "orders", pubsubName = "order_pub_sub")
|
||||||
@PostMapping(path = "/checkout")
|
@PostMapping(path = "/checkout")
|
||||||
public Mono<Void> getCheckout(@RequestBody(required = false) CloudEvent<String> cloudEvent) {
|
public Mono<ResponseEntity> getCheckout(@RequestBody(required = false) CloudEvent<String> cloudEvent) {
|
||||||
return Mono.fromRunnable(() -> {
|
return Mono.fromSupplier(() -> {
|
||||||
try {
|
try {
|
||||||
log.info("Subscriber received: " + cloudEvent.getData());
|
log.info("Subscriber received: " + cloudEvent.getData());
|
||||||
|
return new ResponseEntity<>("successful",
|
||||||
|
HttpStatus.OK);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ start().catch((e) => {
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
async function start(orderId) {
|
async function start(orderId, response) {
|
||||||
const PUBSUB_NAME = "order_pub_sub"
|
const PUBSUB_NAME = "order_pub_sub"
|
||||||
const TOPIC_NAME = "orders"
|
const TOPIC_NAME = "orders"
|
||||||
const server = new DaprServer(
|
const server = new DaprServer(
|
||||||
|
|
@ -19,8 +19,10 @@ async function start(orderId) {
|
||||||
process.env.DAPR_HTTP_PORT,
|
process.env.DAPR_HTTP_PORT,
|
||||||
CommunicationProtocolEnum.HTTP
|
CommunicationProtocolEnum.HTTP
|
||||||
);
|
);
|
||||||
await server.pubsub.subscribe(PUBSUB_NAME, TOPIC_NAME, async (orderId) => {
|
await server.pubsub.subscribe(PUBSUB_NAME, TOPIC_NAME, async (orderId) => {
|
||||||
console.log(`Subscriber received: ${JSON.stringify(orderId)}`)
|
console.log(`Subscriber received: ${JSON.stringify(orderId)}`)
|
||||||
|
}, (response) => {
|
||||||
|
response.sendStatus(200);
|
||||||
});
|
});
|
||||||
await server.startServer();
|
await server.startServer();
|
||||||
}
|
}
|
||||||
|
|
@ -12,5 +12,6 @@ logging.basicConfig(level = logging.INFO)
|
||||||
def mytopic(event: v1.Event) -> None:
|
def mytopic(event: v1.Event) -> None:
|
||||||
data = json.loads(event.Data())
|
data = json.loads(event.Data())
|
||||||
logging.info('Subscriber received: ' + str(data))
|
logging.info('Subscriber received: ' + str(data))
|
||||||
|
return '', 200
|
||||||
|
|
||||||
app.run(6002)
|
app.run(6002)
|
||||||
Loading…
Reference in New Issue