lib.KafkaService

Here are the examples of the java api lib.KafkaService taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

3 Examples 7

19 Source : App.java
with MIT License
from PacktPublishing

@EnableKafka
@SpringBootApplication
@ComponentScan(basePackages = "lib")
public clreplaced App {

    @Autowired
    RedisService redis;

    @Autowired
    KafkaService kafka;

    @Autowired
    Tracer tracer;

    @Bean
    public AppId appId() {
        return new AppId("storage-service");
    }

    @KafkaListener(topics = "message")
    public void process(@Payload Message message, @Headers MessageHeaders headers) throws Exception {
        Span span = kafka.startConsumerSpan("process", headers);
        try (Scope scope = tracer.scopeManager().activate(span, true)) {
            System.out.println("Received message: " + message.message);
            redis.addMessage(message);
            System.out.println("Added message to room.");
        }
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(App.clreplaced, args);
    }
}

19 Source : App.java
with MIT License
from PacktPublishing

@EnableKafka
@SpringBootApplication
@ComponentScan(basePackages = "lib")
public clreplaced App {

    @Bean
    public AppId appId() {
        return new AppId("giphy-service");
    }

    @Autowired
    GiphyService giphy;

    @Autowired
    KafkaService kafka;

    @Autowired
    Tracer tracer;

    @KafkaListener(topics = "message")
    public void process(@Payload Message message, @Headers MessageHeaders headers) throws Exception {
        Span span = kafka.startConsumerSpan("process", headers);
        try (Scope scope = tracer.scopeManager().activate(span, true)) {
            System.out.println("Received message: " + message.message);
            if (message.image == null && message.message.trim().startsWith("/giphy")) {
                String query = message.message.split("/giphy")[1].trim();
                System.out.println("Giphy requested: " + query);
                message.image = giphy.query(query);
                if (message.image != null) {
                    kafka.sendMessage(message);
                    System.out.println("Updated message, url=" + message.image);
                }
            }
        }
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(App.clreplaced, args);
    }
}

19 Source : ChatController.java
with MIT License
from PacktPublishing

@CrossOrigin(maxAge = 3600)
@RestController
public clreplaced ChatController {

    @Autowired
    KafkaTemplate<String, Message> kafkaTemplate;

    @Autowired
    RedisService redis;

    @Autowired
    KafkaService kafka;

    @Bean
    public Executor asyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(2);
        executor.setMaxPoolSize(2);
        executor.setQueueCapacity(10);
        executor.setThreadNamePrefix("send-to-kafka-");
        executor.initialize();
        return executor;
    }

    Executor executor1 = asyncExecutor();

    @Autowired
    Executor executor2;

    private String sendMode = System.getProperty("KSEND");

    @RequestMapping(value = "/message", method = RequestMethod.GET)
    @ResponseBody
    public List<Message> getMessages(@RequestParam(value = "room", defaultValue = "lobby") String room) throws Exception {
        List<Message> messages = redis.getMessages(room);
        System.out.println("Retrieved " + messages.size() + " messages.");
        return messages;
    }

    @RequestMapping(value = "/message", consumes = { "application/json" }, produces = { MediaType.APPLICATION_JSON_VALUE }, method = RequestMethod.POST)
    public ResponseEnreplacedy<Message> postMessage(@RequestBody Message msg) throws Exception {
        msg.init();
        System.out.println("Received message: " + msg);
        if ("async1".equals(sendMode)) {
            kafka.sendMessageAsync(msg, executor1);
            System.out.println("Message sent async (executor1) to Kafka");
        } else if ("async2".equals(sendMode)) {
            kafka.sendMessageAsync(msg, executor2);
            System.out.println("Message sent async (executor2) to Kafka");
        } else {
            kafka.sendMessage(msg);
            System.out.println("Message sent sync to Kafka");
        }
        return new ResponseEnreplacedy<Message>(msg, HttpStatus.OK);
    }
}