10 Ağustos 2020 Pazartesi

Camel ProducerTemplate Sınıfı

Giriş
Şu satırı dahil ederiz
import org.apache.camel.ProducerTemplate;
Başka bir Camel bileşenine mesaj göndermek için kullanılır. Camel ve Rest Enpoint'i birleştirmek için iki tane seçenek var
1. Spring RestController tanımlanır. Bu RestController içinden ProducerTemplate ile Camel çağrısı yapılır
2. Camel içinden Rest Endpoint açılır ve Camel kodlarıyla devam edilir.

asyncSendBody metodu
Şöyle yaparız
import org.apache.camel.Produce;
import org.apache.camel.ProducerTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.javainuse.model.Employee;


@RestController
public class SpringRabbitMQController {

  @Produce(uri = "direct:startRabbitMQPoint")
  private ProducerTemplate template;

  @RequestMapping(value = "/employee", method = RequestMethod.GET)
  public String createEmployee(@RequestParam int id, @RequestParam String name,
    @RequestParam String designation) {

    Employee emp = new Employee();
    emp.setName(name);
    emp.setDesignation(designation);
    emp.setEmpId(id);

    template.asyncSendBody(template.getDefaultEndpoint(), emp);
    return "";
  }
}
requestBody metodu
Şöyle yaparız
@RestController
@RequestMapping("/alert/v1")
@CrossOrigin(origins = "*", maxAge = 6000)
public class HelloWorldController {

  @Autowired
  private ProducerTemplate template;

  Gson gson = new Gson();

  @GetMapping(value = "/hello", produces = "application/json")
  public ResponseEntity < String > sayHello() {
    try {
      return ResponseEntity.status(HttpStatus.OK)
        .body(gson.toJson(template.requestBody("direct:sayHello", "Hello from rest 
                 controller")));
    } catch (Exception e) {
      return ResponseEntity.status(HttpStatus.OK).body(null);
    }
  }
}

Hiç yorum yok:

Yorum Gönder