Search in sources :

Example 1 with KafkaListener

use of org.springframework.kafka.annotation.KafkaListener in project open-kilda by telstra.

the class KafkaMessageConsumer method receive.

/**
 * Receives messages from WorkFlowManager queue.
 *
 * @param record the message object instance
 */
@KafkaListener(topics = "kilda-test")
public void receive(final String record) {
    logger.debug("message received: {}", record);
    try {
        Message message = MAPPER.readValue(record, Message.class);
        if (message.getDestination() == null || Destination.TOPOLOGY_ENGINE.equals(message.getDestination())) {
            if (message instanceof CommandMessage) {
                CommandData data = ((CommandMessage) message).getData();
                if (data instanceof FlowCreateRequest) {
                    FlowPayload payload = ((FlowCreateRequest) data).getPayload();
                    logger.debug("FlowCreateRequest: {}", payload);
                    Set<CommandMessage> commands = flowService.createFlow(payload, message.getCorrelationId());
                    for (CommandMessage response : commands) {
                        kafkaMessageProducer.send(topic, response);
                    }
                    logger.debug("Response send, {}={}", CORRELATION_ID, message.getCorrelationId());
                } else if (data instanceof FlowDeleteRequest) {
                    FlowIdStatusPayload payload = ((FlowDeleteRequest) data).getPayload();
                    logger.debug("FlowDeleteRequest: {}", payload);
                    Set<CommandMessage> commands = flowService.deleteFlow(payload, message.getCorrelationId());
                    for (CommandMessage response : commands) {
                        kafkaMessageProducer.send(topic, response);
                    }
                    logger.debug("Response send, {}={}", CORRELATION_ID, message.getCorrelationId());
                } else if (data instanceof FlowUpdateRequest) {
                    FlowPayload payload = ((FlowUpdateRequest) data).getPayload();
                    logger.debug("FlowUpdateRequest: {}", payload);
                    Set<CommandMessage> commands = flowService.updateFlow(payload, message.getCorrelationId());
                    for (CommandMessage response : commands) {
                        kafkaMessageProducer.send(topic, response);
                    }
                    logger.debug("Response send, {}={}", CORRELATION_ID, message.getCorrelationId());
                } else if (data instanceof FlowGetRequest) {
                    FlowIdStatusPayload payload = ((FlowGetRequest) data).getPayload();
                    logger.debug("FlowGetRequest: {}", payload);
                    InfoMessage response = flowService.getFlow(payload, message.getCorrelationId());
                    kafkaMessageProducer.send(topic, response);
                    logger.debug("Response send, {}={}", CORRELATION_ID, message.getCorrelationId());
                } else if (data instanceof FlowsGetRequest) {
                    FlowIdStatusPayload payload = ((FlowsGetRequest) data).getPayload();
                    logger.debug("FlowsGetRequest: {}", payload);
                    InfoMessage response = flowService.getFlows(payload, message.getCorrelationId());
                    kafkaMessageProducer.send(topic, response);
                    logger.debug("Response send, {}={}", CORRELATION_ID, message.getCorrelationId());
                } else if (data instanceof FlowPathRequest) {
                    FlowIdStatusPayload payload = ((FlowPathRequest) data).getPayload();
                    logger.debug("FlowPathRequest: {}", payload);
                    InfoMessage response = flowService.pathFlow(payload, message.getCorrelationId());
                    kafkaMessageProducer.send(topic, response);
                    logger.debug("Response send, {}={}", CORRELATION_ID, message.getCorrelationId());
                } else {
                    logger.error("Unexpected command message data type: {}", data);
                }
            } else if (message instanceof InfoMessage) {
                InfoData data = ((InfoMessage) message).getData();
                if (data instanceof SwitchInfoData) {
                    SwitchInfoData payload = (SwitchInfoData) data;
                    switch(payload.getState()) {
                        case ADDED:
                            switchService.add(payload);
                            break;
                        case ACTIVATED:
                            switchService.activate(payload);
                            break;
                        case DEACTIVATED:
                            switchService.deactivate(payload);
                            break;
                        case REMOVED:
                            switchService.remove(payload);
                            break;
                        case CHANGED:
                        default:
                            break;
                    }
                } else if (data instanceof IslInfoData) {
                    IslInfoData payload = (IslInfoData) data;
                    islService.discoverLink(payload);
                } else {
                    logger.debug("Unexpected info message data type: {}", data);
                }
            }
        } else {
            logger.debug("Skip message: {}", message);
        }
    } catch (IOException exception) {
        logger.error("Could not deserialize message: {}", record, exception);
    }
}
Also used : FlowsGetRequest(org.openkilda.messaging.command.flow.FlowsGetRequest) Set(java.util.Set) FlowUpdateRequest(org.openkilda.messaging.command.flow.FlowUpdateRequest) InfoMessage(org.openkilda.messaging.info.InfoMessage) Message(org.openkilda.messaging.Message) CommandMessage(org.openkilda.messaging.command.CommandMessage) FlowIdStatusPayload(org.openkilda.messaging.payload.flow.FlowIdStatusPayload) FlowDeleteRequest(org.openkilda.messaging.command.flow.FlowDeleteRequest) FlowCreateRequest(org.openkilda.messaging.command.flow.FlowCreateRequest) FlowGetRequest(org.openkilda.messaging.command.flow.FlowGetRequest) IOException(java.io.IOException) CommandMessage(org.openkilda.messaging.command.CommandMessage) FlowPayload(org.openkilda.messaging.payload.flow.FlowPayload) InfoMessage(org.openkilda.messaging.info.InfoMessage) IslInfoData(org.openkilda.messaging.info.event.IslInfoData) SwitchInfoData(org.openkilda.messaging.info.event.SwitchInfoData) InfoData(org.openkilda.messaging.info.InfoData) IslInfoData(org.openkilda.messaging.info.event.IslInfoData) CommandData(org.openkilda.messaging.command.CommandData) SwitchInfoData(org.openkilda.messaging.info.event.SwitchInfoData) FlowPathRequest(org.openkilda.messaging.command.flow.FlowPathRequest) KafkaListener(org.springframework.kafka.annotation.KafkaListener)

Example 2 with KafkaListener

use of org.springframework.kafka.annotation.KafkaListener in project open-kilda by telstra.

the class KafkaMessageConsumer method receive.

/**
 * Receives messages from WorkFlowManager queue.
 *
 * @param record the message object instance
 */
@KafkaListener(id = "northbound-listener", topics = Topic.NORTHBOUND)
public void receive(final String record) {
    try {
        logger.trace("message received");
        Message message = MAPPER.readValue(record, Message.class);
        if (Destination.NORTHBOUND.equals(message.getDestination())) {
            logger.debug("message received: {}", record);
            messages.put(message.getCorrelationId(), message);
        } else {
            logger.trace("Skip message: {}", message);
        }
    } catch (IOException exception) {
        logger.error("Could not deserialize message: {}", record, exception);
    }
}
Also used : Message(org.openkilda.messaging.Message) IOException(java.io.IOException) KafkaListener(org.springframework.kafka.annotation.KafkaListener)

Example 3 with KafkaListener

use of org.springframework.kafka.annotation.KafkaListener in project summer by foxsugar.

the class UserServiceMsgConsumer method listen.

@KafkaListener(id = "userService", topicPattern = "userService")
public void listen(ConsumerRecord<String, String> record) {
    ThreadPool.getInstance().executor.execute(() -> {
        String key = record.key();
        String value = record.value();
        KafkaMsgKey msgKey = JsonUtil.readValue(key, KafkaMsgKey.class);
        JsonNode msgValue = JsonUtil.readTree(value);
        UserServiceMsgDispatch userServiceMsgDispatch = SpringUtil.getBean(UserServiceMsgDispatch.class);
        userServiceMsgDispatch.dispatchMsg(msgKey, msgValue);
    });
}
Also used : KafkaMsgKey(com.code.server.constant.kafka.KafkaMsgKey) JsonNode(com.fasterxml.jackson.databind.JsonNode) UserServiceMsgDispatch(com.code.server.login.service.UserServiceMsgDispatch) KafkaListener(org.springframework.kafka.annotation.KafkaListener)

Example 4 with KafkaListener

use of org.springframework.kafka.annotation.KafkaListener in project summer by foxsugar.

the class UserServiceMsgConsumer method listen3.

@KafkaListener(id = "clubService", topicPattern = "clubService")
public void listen3(ConsumerRecord<String, String> record) {
    String key = record.key();
    String value = record.value();
    KafkaMsgKey msgKey = JsonUtil.readValue(key, KafkaMsgKey.class);
    JsonNode msgValue = JsonUtil.readTree(value);
    ClubServiceMsgDispatch clubServiceMsgDispatch = SpringUtil.getBean(ClubServiceMsgDispatch.class);
    clubServiceMsgDispatch.dispatchMsg(msgKey, msgValue);
}
Also used : ClubServiceMsgDispatch(com.code.server.login.service.ClubServiceMsgDispatch) KafkaMsgKey(com.code.server.constant.kafka.KafkaMsgKey) JsonNode(com.fasterxml.jackson.databind.JsonNode) KafkaListener(org.springframework.kafka.annotation.KafkaListener)

Example 5 with KafkaListener

use of org.springframework.kafka.annotation.KafkaListener in project micro-service by Zephery.

the class MsgConsumer method processMessage.

@KafkaListener(topics = { "nginx-access-log" })
public void processMessage(String content) {
    JsonObject object = parser.parse(content).getAsJsonObject();
    String upstreamhost = object.get("upstreamhost").getAsString();
    map.put(upstreamhost, map.get("upstreamhost") == null ? 1 : map.get("upstreamhost") + 1);
    for (Map.Entry<String, Integer> entry : map.entrySet()) {
        System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
    }
}
Also used : JsonObject(com.google.gson.JsonObject) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) KafkaListener(org.springframework.kafka.annotation.KafkaListener)

Aggregations

KafkaListener (org.springframework.kafka.annotation.KafkaListener)13 KafkaMsgKey (com.code.server.constant.kafka.KafkaMsgKey)3 EventStoreException (com.kloia.eventapis.exception.EventStoreException)3 IOException (java.io.IOException)3 Message (org.openkilda.messaging.Message)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 StockReservedEvent (com.kloia.sample.dto.event.StockReservedEvent)2 Orders (com.kloia.sample.model.Orders)2 Stock (com.kloia.sample.model.Stock)2 InfoMessage (org.openkilda.messaging.info.InfoMessage)2 ClubServiceMsgDispatch (com.code.server.login.service.ClubServiceMsgDispatch)1 UserServiceMsgDispatch (com.code.server.login.service.UserServiceMsgDispatch)1 JsonObject (com.google.gson.JsonObject)1 EventKey (com.kloia.eventapis.common.EventKey)1 Operation (com.kloia.eventapis.pojos.Operation)1 AggregateListener (com.kloia.eventapis.view.AggregateListener)1 StockNotEnoughException (com.kloia.sample.dto.StockNotEnoughException)1 ReturnPaymentCommandDto (com.kloia.sample.dto.command.ReturnPaymentCommandDto)1 OrderCancelledEvent (com.kloia.sample.dto.event.OrderCancelledEvent)1 PaymentFailedEvent (com.kloia.sample.dto.event.PaymentFailedEvent)1