當前位置: 首頁>>代碼示例>>Java>>正文


Java KafkaTemplate類代碼示例

本文整理匯總了Java中org.springframework.kafka.core.KafkaTemplate的典型用法代碼示例。如果您正苦於以下問題:Java KafkaTemplate類的具體用法?Java KafkaTemplate怎麽用?Java KafkaTemplate使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


KafkaTemplate類屬於org.springframework.kafka.core包,在下文中一共展示了KafkaTemplate類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: publish

import org.springframework.kafka.core.KafkaTemplate; //導入依賴的package包/類
@Override
public void publish(final Message message, final KafkaTemplate<String, byte[]> kafkaTemplate) {

	try {
		final byte[] effectivePayload = message.getContent().getData();
		final String sourceTopic = message.getDestination().getPhysicalName();
		final String destTopic = this.getDestTopic(sourceTopic);
		kafkaTemplate.send(destTopic, effectivePayload);
	} catch (final Throwable t) {
		LOG.error("Exception occured", t);
	}
}
 
開發者ID:dcsolutions,項目名稱:kalinka,代碼行數:13,代碼來源:MqttSparkClusterJmsMessagePublisher.java

示例2: testAutoCommit

import org.springframework.kafka.core.KafkaTemplate; //導入依賴的package包/類
@Test
public void testAutoCommit() throws Exception {
    LOG.info("Start testAutoCommit");
    ContainerProperties containerProps = new ContainerProperties("topic3", "topic4");
    final CountDownLatch latch = new CountDownLatch(4);
    containerProps.setMessageListener((MessageListener<Integer, String>) message -> {
        LOG.info("received: " + message);
        latch.countDown();
    });
    KafkaMessageListenerContainer<Integer, String> container = createContainer(containerProps,
            IntegerDeserializer.class, StringDeserializer.class);
    container.setBeanName("testAutoCommit");
    container.start();
    Thread.sleep(5000); // wait a bit for the container to start
    KafkaTemplate<Integer, String> template = createTemplate(IntegerSerializer.class, StringSerializer.class);
    template.setDefaultTopic("topic3");
    template.sendDefault(0, "foo");
    template.sendDefault(2, "bar");
    template.sendDefault(0, "baz");
    template.sendDefault(2, "qux");
    template.flush();
    assertTrue(latch.await(60, TimeUnit.SECONDS));
    container.stop();
    LOG.info("Stop testAutoCommit");
}
 
開發者ID:rmap-project,項目名稱:rmap,代碼行數:26,代碼來源:SimpleKafkaIT.java

示例3: mockTemplate

import org.springframework.kafka.core.KafkaTemplate; //導入依賴的package包/類
@SuppressWarnings("unchecked")
public static <K, V> KafkaTemplate<K, V> mockTemplate()
        throws InterruptedException, ExecutionException, TimeoutException {
    KafkaTemplate<K, V> template = mock(KafkaTemplate.class);
    SettableListenableFuture future = mock(SettableListenableFuture.class);
    when(future.get(30000, TimeUnit.MILLISECONDS)).thenReturn(null);
    when(template.send(any(), any(), any())).thenReturn(future);
    return template;
}
 
開發者ID:rmap-project,項目名稱:rmap,代碼行數:10,代碼來源:MockKafkaTemplateFactory.java

示例4: ProducerConfigurationMessageHandler

import org.springframework.kafka.core.KafkaTemplate; //導入依賴的package包/類
ProducerConfigurationMessageHandler(KafkaTemplate<byte[], byte[]> kafkaTemplate, String topic,
		ExtendedProducerProperties<KafkaProducerProperties> producerProperties,
		ProducerFactory<byte[], byte[]> producerFactory) {
	super(kafkaTemplate);
	setTopicExpression(new LiteralExpression(topic));
	setMessageKeyExpression(producerProperties.getExtension().getMessageKeyExpression());
	setBeanFactory(KafkaMessageChannelBinder.this.getBeanFactory());
	if (producerProperties.isPartitioned()) {
		SpelExpressionParser parser = new SpelExpressionParser();
		setPartitionIdExpression(parser.parseExpression("headers." + BinderHeaders.PARTITION_HEADER));
	}
	if (producerProperties.getExtension().isSync()) {
		setSync(true);
	}
	this.producerFactory = producerFactory;
}
 
開發者ID:spring-cloud,項目名稱:spring-cloud-stream-binder-kafka,代碼行數:17,代碼來源:KafkaMessageChannelBinder.java

示例5: receiveAndValidateFoo

import org.springframework.kafka.core.KafkaTemplate; //導入依賴的package包/類
private void receiveAndValidateFoo(ConfigurableApplicationContext context) throws Exception {
	Map<String, Object> senderProps = KafkaTestUtils.producerProps(embeddedKafka);
	DefaultKafkaProducerFactory<Integer, String> pf = new DefaultKafkaProducerFactory<>(senderProps);
	KafkaTemplate<Integer, String> template = new KafkaTemplate<>(pf, true);
	template.setDefaultTopic("foos");
	template.sendDefault("{\"id\":\"123\"}");
	ConsumerRecord<Integer, String> cr = KafkaTestUtils.getSingleRecord(consumer, "counts-id");

	assertThat(cr.key().equals(123));
	ObjectMapper om = new ObjectMapper();
	Long aLong = om.readValue(cr.value(), Long.class);
	assertThat(aLong.equals(1L));
}
 
開發者ID:spring-cloud,項目名稱:spring-cloud-stream-binder-kafka,代碼行數:14,代碼來源:KStreamBinderPojoInputAndPrimitiveTypeOutputTests.java

示例6: setUp

import org.springframework.kafka.core.KafkaTemplate; //導入依賴的package包/類
@Before
public void setUp() throws Exception {
  // set up the Kafka producer properties
  Map<String, Object> senderProperties =
      KafkaTestUtils.senderProps(AllSpringKafkaTests.embeddedKafka.getBrokersAsString());

  // create a Kafka producer factory
  ProducerFactory<String, String> producerFactory =
      new DefaultKafkaProducerFactory<String, String>(senderProperties);

  // create a Kafka template
  template = new KafkaTemplate<>(producerFactory);
  // set the default topic to send to
  template.setDefaultTopic(AllSpringKafkaTests.RECEIVER_TOPIC);

  // wait until the partitions are assigned
  for (MessageListenerContainer messageListenerContainer : kafkaListenerEndpointRegistry
      .getListenerContainers()) {
    ContainerTestUtils.waitForAssignment(messageListenerContainer,
        AllSpringKafkaTests.embeddedKafka.getPartitionsPerTopic());
  }
}
 
開發者ID:code-not-found,項目名稱:spring-kafka,代碼行數:23,代碼來源:SpringKafkaReceiverTest.java

示例7: setUp

import org.springframework.kafka.core.KafkaTemplate; //導入依賴的package包/類
@Before
public void setUp() throws Exception {
  // set up the Kafka producer properties
  Map<String, Object> senderProperties =
      KafkaTestUtils.senderProps(embeddedKafka.getBrokersAsString());

  // create a Kafka producer factory
  ProducerFactory<String, String> producerFactory =
      new DefaultKafkaProducerFactory<String, String>(senderProperties);

  // create a Kafka template
  template = new KafkaTemplate<>(producerFactory);
  // set the default topic to send to
  template.setDefaultTopic(RECEIVER_TOPIC);

  // wait until the partitions are assigned
  for (MessageListenerContainer messageListenerContainer : kafkaListenerEndpointRegistry
      .getListenerContainers()) {
    ContainerTestUtils.waitForAssignment(messageListenerContainer,
        embeddedKafka.getPartitionsPerTopic());
  }
}
 
開發者ID:code-not-found,項目名稱:spring-kafka,代碼行數:23,代碼來源:SpringKafkaReceiverTest.java

示例8: IgniteOperationRepository

import org.springframework.kafka.core.KafkaTemplate; //導入依賴的package包/類
@Autowired
public IgniteOperationRepository(@Qualifier("operationIgniteClient") Ignite ignite, KafkaTemplate kafkaTemplate) {
    this.ignite = ignite;
    operationCache = ignite.cache("operationCache");

    this.kafkaTemplate = kafkaTemplate;
}
 
開發者ID:kloiasoft,項目名稱:eventapis,代碼行數:8,代碼來源:IgniteOperationRepository.java

示例9: setup

import org.springframework.kafka.core.KafkaTemplate; //導入依賴的package包/類
@Before
public void setup() throws Exception {
	Map<String, Object> consumerProps = KafkaTestUtils.consumerProps("testT", "false", embeddedKafka);
	DefaultKafkaConsumerFactory<String, String> cf =
		new DefaultKafkaConsumerFactory<>(consumerProps);
	ContainerProperties containerProperties = new ContainerProperties(TEST_TOPIC);
	container = new KafkaMessageListenerContainer<>(cf, containerProperties);
	final BlockingQueue<ConsumerRecord<String, String>> records = new LinkedBlockingQueue<>();
	container.setupMessageListener((MessageListener<String, String>) record -> {
           log.error("Message received: " + record);
           records.add(record);
       });
	container.start();
	ContainerTestUtils.waitForAssignment(container, embeddedKafka.getPartitionsPerTopic());
	Map<String, Object> senderProps = KafkaTestUtils.senderProps(embeddedKafka.getBrokersAsString());
	ProducerFactory<String, String> pf =
		new DefaultKafkaProducerFactory<>(senderProps);
	template = new KafkaTemplate<>(pf);
	template.setDefaultTopic(TEST_TOPIC);
}
 
開發者ID:underscorenico,項目名稱:skeleton-oms-java,代碼行數:21,代碼來源:HelloProcessTest.java

示例10: publish

import org.springframework.kafka.core.KafkaTemplate; //導入依賴的package包/類
@Override
public void publish(final Message message, final KafkaTemplate<String, byte[]> kafkaTemplate) {

	try {
		final byte[] effectivePayload = message.getContent().getData();
		final MessageContainer messageContainer = this.createMessageContainer(message.getDestination().getPhysicalName(), effectivePayload);
		LOG.debug("Will send message={}", messageContainer);
		kafkaTemplate.send(messageContainer.topic, messageContainer.key, messageContainer.content);
	} catch (final Throwable t) {
		LOG.error("Exception occured", t);
	}
}
 
開發者ID:dcsolutions,項目名稱:kalinka,代碼行數:13,代碼來源:MqttMqttJmsMessagePublisher.java

示例11: publish

import org.springframework.kafka.core.KafkaTemplate; //導入依賴的package包/類
@Override
public void publish(final Message message, final KafkaTemplate<String, byte[]> kafkaTemplate) {

	try {
		final byte[] effectivePayload = JmsUtil.getPayload((BytesMessage) message);
		final MessageContainer messageContainer = this.createMessageContainer(message.getStringProperty("JMSDestination"), effectivePayload);
		LOG.debug("Will send message={}", messageContainer);
		kafkaTemplate.send(messageContainer.topic, messageContainer.key, messageContainer.content);
	} catch (final Throwable t) {
		LOG.error("Exception occured", t);
	}
}
 
開發者ID:dcsolutions,項目名稱:kalinka,代碼行數:13,代碼來源:MqttMqttJmsMessagePublisher.java

示例12: publish

import org.springframework.kafka.core.KafkaTemplate; //導入依賴的package包/類
@Override
public void publish(final Message message, final KafkaTemplate<String, byte[]> kafkaTemplate) {

	try {
		final byte[] effectivePayload = JmsUtil.getPayload((BytesMessage) message);
		final String sourceTopic = message.getStringProperty("JMSDestination");
		final String destTopic = this.getDestTopic(sourceTopic);
		kafkaTemplate.send(destTopic, effectivePayload);
	} catch (final Throwable t) {
		LOG.error("Exception occured", t);
	}
}
 
開發者ID:dcsolutions,項目名稱:kalinka,代碼行數:13,代碼來源:MqttSparkClusterJmsMessagePublisher.java

示例13: start

import org.springframework.kafka.core.KafkaTemplate; //導入依賴的package包/類
@Override
public void start() {
    if (null == this.keyStrategy) {
        this.keyStrategy = new DefaultKeyStrategy();
    }
    this.addProducerConfigValue(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, ByteArraySerializer.class);
    this.addProducerConfigValue(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, ByteArraySerializer.class);
    ProducerFactory<byte[], byte[]> producerFactory = new DefaultKafkaProducerFactory<>(this.producerConfig);
    this.kafkaTemplate = new KafkaTemplate<>(producerFactory);
    this.kafkaTemplate.setDefaultTopic(this.topic.trim());
    super.start();
}
 
開發者ID:ilifan,項目名稱:smart-framework,代碼行數:13,代碼來源:KafkaAppender.java

示例14: createTemplate

import org.springframework.kafka.core.KafkaTemplate; //導入依賴的package包/類
private <T> KafkaTemplate<Integer, T> createTemplate(Object keySer, Object valSer) {
    Map<String, Object> senderProps = senderProps(keySer, valSer);
    ProducerFactory<Integer, T> pf =
            new DefaultKafkaProducerFactory<>(senderProps);
    KafkaTemplate<Integer, T> template = new KafkaTemplate<>(pf);
    return template;
}
 
開發者ID:rmap-project,項目名稱:rmap,代碼行數:8,代碼來源:SimpleKafkaIT.java

示例15: receiveAndValidateFoo

import org.springframework.kafka.core.KafkaTemplate; //導入依賴的package包/類
private void receiveAndValidateFoo(ConfigurableApplicationContext context) throws Exception {
	Map<String, Object> senderProps = KafkaTestUtils.producerProps(embeddedKafka);
	DefaultKafkaProducerFactory<Integer, String> pf = new DefaultKafkaProducerFactory<>(senderProps);
	KafkaTemplate<Integer, String> template = new KafkaTemplate<>(pf, true);
	template.setDefaultTopic("foos");
	template.sendDefault("{\"id\":\"123\"}");
	ConsumerRecord<String, String> cr = KafkaTestUtils.getSingleRecord(consumer, "counts-id");
	assertThat(cr.value().contains("Count for product with ID 123: 1")).isTrue();
}
 
開發者ID:spring-cloud,項目名稱:spring-cloud-stream-binder-kafka,代碼行數:10,代碼來源:KstreamBinderPojoInputStringOutputIntegrationTests.java


注:本文中的org.springframework.kafka.core.KafkaTemplate類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。