29 Temmuz 2021 Perşembe

SpringCloud Stream @Output Anotasyonu - Kullanmayın

Giriş
@Output anotasyonu yerine StreamBridge sınıfı da kullanılabilir.

Örnek
Şeklen şöyle
application.yaml şöyle olsun
spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
  cloud:
    stream:
      bindings:
        orderSubmissionOutput:
          destination: orderSubmitted.exchange
Şöyle yaparız. Böylece topic'e mesaj yazılır
public interface MessageChannels {

  static final String ORDER_SUBMISSION_OUTPUT = "orderSubmissionOutput";

  @Output (ORDER_SUBMISSION_OUTPUT)
  MessageChannel orderSubmissionChannel();
}


@MessagingGateway
public interface OrderSubmissionGateway {

  @Gateway(requestChannel = MessageChannels.ORDER_SUBMISSION_OUTPUT)
  void submitOrder(Order order);

}

@RestController
@RequestMapping("/orders")
public class OrderRestController {

  @Autowired
  private OrderSubmissionGateway orderSubmissionGateway;

  @PostMapping()
  public ResponseEntity<Order> submitOrder(@RequestBody @Valid Order order) {
    Order orderToBeSubmitted = order.withSubmissionDate(Instant.now());
    orderSubmissionGateway.submitOrder(orderToBeSubmitted);
    return ResponseEntity.ok(orderToBeSubmitted);
  }
}
Örnek - Producer
Şöyle yaparız. Burada @Output ile Source arayüzünün output isimli topic'e bir şey yazacağını belirtiriz.
public interface Source {

  String OUTPUT = "output";

  @Output(Source.OUTPUT)
  MessageChannel output();

}
application.properties şöyledir
cloud.stream:
 bindings.input:
   destination: payment-approval-topic
   group: payment-service-consumer
 bindings.output:
   destination: payment-notification-topic
   contentType: application/json
Kullanmak için şöyle yaparız
@EnableBinding({Source.class})
public class SubscriptionRequestsProducer {
  private final Source source;

  public SubscriptionRequestsProducer(Source source) {
    this.source = source;
  }

  public void requestApproval(Map<String, Object> subscriptionRequest) {
    source.output().send(MessageBuilder.withPayload(subscriptionRequest).build());
  }
}
Örnek - Processor
Şöyle yaparız
public interface MyProcessor {
   String INPUT = "myInput";

   @Input
   SubscribableChannel myInput();

   @Output("myOutput")
   MessageChannel anOutput();

   @Output
   MessageChannel anotherOutput();
}

Hiç yorum yok:

Yorum Gönder