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
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.
application.properties şöyledirpublic interface Source {String OUTPUT = "output";@Output(Source.OUTPUT)MessageChannel output();}
Kullanmak için şöyle yaparızcloud.stream:bindings.input:destination: payment-approval-topicgroup: payment-service-consumerbindings.output:destination: payment-notification-topiccontentType: application/json
Örnek - Processor@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());}}
Şöyle yaparız
public interface MyProcessor {String INPUT = "myInput";@InputSubscribableChannel myInput();@Output("myOutput")MessageChannel anOutput();@OutputMessageChannel anotherOutput();}
Hiç yorum yok:
Yorum Gönder