本文整理汇总了Java中org.springframework.kafka.annotation.KafkaListener类的典型用法代码示例。如果您正苦于以下问题:Java KafkaListener类的具体用法?Java KafkaListener怎么用?Java KafkaListener使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
KafkaListener类属于org.springframework.kafka.annotation包,在下文中一共展示了KafkaListener类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: receive
import org.springframework.kafka.annotation.KafkaListener; //导入依赖的package包/类
/**
* Receives messages from WorkFlowManager queue.
*
* @param record the message object instance
*/
@KafkaListener(id = "northbound-listener-health-check", topics = Topic.HEALTH_CHECK)
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);
InfoMessage info = (InfoMessage) message;
HealthCheckInfoData healthCheck = (HealthCheckInfoData) info.getData();
messages.put(healthCheck.getId(), healthCheck);
} else {
logger.trace("Skip message: {}", message);
}
} catch (IOException exception) {
logger.error("Could not deserialize message: {}", record, exception);
}
}
示例2: onMessage
import org.springframework.kafka.annotation.KafkaListener; //导入依赖的package包/类
/**
* Read the topic and push the data to the DB The @KafkaListener annotation
* provides a mechanism for simple POJO listeners
*/
@Override
@KafkaListener(id = "consumer", topics = "#{'${kafka.topics}'.split('\\\\ ')}")
public void onMessage(ConsumerRecord<String, Student> data, Acknowledgment acknowledgment) {
try {
LOGGER.info("Record value is : " + data.value());
LOGGER.info("Offset value is : " + data.offset());
LOGGER.info("Topic is : " + data.topic());
LOGGER.info("Partition is : " + data.partition());
LOGGER.info("printing the calendar object:" + data.value());
service.create(data.value());
LOGGER.info("pushed the data to DB successfuly");
} catch (Exception e) {
LOGGER.error("Push the messaged to Error Stream : " + e);
} finally {
acknowledgment.acknowledge();
}
}
示例3: on
import org.springframework.kafka.annotation.KafkaListener; //导入依赖的package包/类
/**
* Updates the current stock by reducing the inventory items by the amount of the individual line items in the order.
*
* @param message will never be {@literal null}.
*/
@Transactional
@KafkaListener(topics = "orders")
void on(OrderCompleted message) {
log.info("Order completed: {}", message.getOrderId().toString());
message.getLineItems().stream() //
.peek(it -> log.info("Decreasing quantity of product {} by {}.", it.getProductNumber(), it.getQuantity()))
.forEach(it -> inventory.updateInventoryItem(it.getProductNumber(), it.getQuantity()));
}
示例4: kafkaListener
import org.springframework.kafka.annotation.KafkaListener; //导入依赖的package包/类
@KafkaListener(topics = {"inventory"})
public void kafkaListener(ConsumerRecord<?, byte[]> record) {
Optional<?> messages = Optional.ofNullable(record.value());
if (messages.isPresent()) {
byte[] msg = (byte[]) messages.get();
LogUtil.debug(LOGGER, "接收到Myth分布式框架消息对象:{}", () -> msg);
mythMqReceiveService.processMessage(msg);
}
}
示例5: receive
import org.springframework.kafka.annotation.KafkaListener; //导入依赖的package包/类
/**
* 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);
}
}
示例6: onProductAdded
import org.springframework.kafka.annotation.KafkaListener; //导入依赖的package包/类
/**
* Creates a new {@link InventoryItem} for the product that was added.
*
* @param message will never be {@literal null}.
*/
@KafkaListener(topics = "products")
public void onProductAdded(ProductAdded message) throws IOException {
Optional<InventoryItem> item = inventory.findByProductId(message.getProductId());
if (item.isPresent()) {
log.info("Inventory item for product {} already available!", message.getProductId());
return;
}
log.info("Creating inventory item for product {}.", message.getProductId());
item.orElseGet(() -> inventory.save(InventoryItem.of(message.getProductId(), message.getName(), 0L)));
}
示例7: kafkaListener
import org.springframework.kafka.annotation.KafkaListener; //导入依赖的package包/类
@KafkaListener(topics = {"account"})
public void kafkaListener(ConsumerRecord<?, byte[]> record) {
Optional<?> messages = Optional.ofNullable(record.value());
if (messages.isPresent()) {
byte[] msg = (byte[]) messages.get();
LogUtil.debug(LOGGER, "接收到Myth分布式框架消息对象:{}", () -> msg);
mythMqReceiveService.processMessage(msg);
}
}
示例8: listenOperations
import org.springframework.kafka.annotation.KafkaListener; //导入依赖的package包/类
@KafkaListener(id = "op-listener", topics = "operation-events", containerFactory = "operationsKafkaListenerContainerFactory")
private void listenOperations(ConsumerRecord<String, Operation> data) throws EventStoreException {
log.info("Incoming Message: " + data.key()+ " "+ data.value());
for (AggregateListener snapshotRecorder : aggregateListeners) {
snapshotRecorder.listenOperations(data);
}
}
示例9: execute
import org.springframework.kafka.annotation.KafkaListener; //导入依赖的package包/类
@Override
@KafkaListener(topics = "PaymentSuccessEvent", containerFactory = "eventsKafkaListenerContainerFactory")
public EventKey execute(PaymentSuccessEvent dto) throws EventStoreException, EventPulisherException, ConcurrentEventException {
Order order = orderQuery.queryEntity(dto.getOrderId());
if (order.getState() == OrderState.PAYMENT_READY) {
log.info("Payment is processing : " + dto);
return eventRepository.recordAndPublish(order.getEventKey(),new OrderPaidEvent(dto.getPaymentId()));
} else
throw new EventStoreException("Order state is not valid for this Operation: " + dto);
}
示例10: execute
import org.springframework.kafka.annotation.KafkaListener; //导入依赖的package包/类
@Override
@KafkaListener(topics = "StockReservedEvent", containerFactory = "eventsKafkaListenerContainerFactory")
public EventKey execute(StockReservedEvent dto) throws EventStoreException, EventPulisherException, ConcurrentEventException {
Order order = orderQuery.queryEntity(dto.getOrderId());
if (order.getState() == OrderState.PROCESSING) {
PaymentProcessEvent paymentProcessEvent = new PaymentProcessEvent(order.getId(),new PaymentInformation(order.getPaymentAddress(),order.getAmount(),order.getCardInformation()));
log.info("Payment is processing : " + dto);
return eventRepository.recordAndPublish(order,paymentProcessEvent);
} else
throw new EventStoreException("Order state is not valid for this Operation: " + dto);
}
示例11: listen
import org.springframework.kafka.annotation.KafkaListener; //导入依赖的package包/类
@KafkaListener(id = "foo", topics = "topic1", group = "group1")
public void listen(ConsumerRecord<?, ?> record) {
log.debug("Checkout launched externally with message: " + record.toString());
// The order id would be deserialized from the received message
String orderId = "Fake order id";
Map<String, Object> variables = new HashMap<>();
variables.put("orderId", orderId);
runtimeService.startProcessInstanceByKey("hello", variables);
}
示例12: listen
import org.springframework.kafka.annotation.KafkaListener; //导入依赖的package包/类
@KafkaListener(topics = "hello.t")
public void listen(String data) {
// Retrieve the process instance id (execution id key in the message)
String processInstanceId = data;
log.debug("Resume process instance: " + processInstanceId);
// Signal (resume) execution
runtimeService.signal(processInstanceId);
}
示例13: testListener
import org.springframework.kafka.annotation.KafkaListener; //导入依赖的package包/类
@KafkaListener(topics = {"bootcwenaoTopic"})
public void testListener(ConsumerRecord<?, ?> record) {
Optional<?> messages = Optional.ofNullable(record.value());
if (messages.isPresent()) {
Object msg = messages.get();
System.out.println(" this is the testTopic send message: " + msg);
}
}
示例14: listen
import org.springframework.kafka.annotation.KafkaListener; //导入依赖的package包/类
@KafkaListener(topics = {"test-topic"})
public void listen(ConsumerRecord<?, ?> record) {
logger.debug("listen1 ");
Optional<?> kafkaMessage = Optional.ofNullable(record.value());
if (kafkaMessage.isPresent()) {
Object message = kafkaMessage.get();
logger.debug("listen1 : {}", message);
}
}
示例15: onMessage
import org.springframework.kafka.annotation.KafkaListener; //导入依赖的package包/类
/**
* Message consumer handle.<BR/>
*
* @param record message object
*/
@KafkaListener(topics = "tracking")
public void onMessage(ConsumerRecord<String, String> record) {
log.debug("received message: {}", record);
try {
TrackingEntity tracking = JsonMapper.readJsonObject(record.value(), TrackingEntity.class);
// trackingService.insert(tracking);
// JPA
//trackingRepository.save(tracking);
databaseManager.createTracking(tracking);
} catch (Exception e) {
log.error(e.getMessage());
}
}