org.springframework.kafka.core.ProducerFactory Java Examples

The following examples show how to use org.springframework.kafka.core.ProducerFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SpringKafkaReceiverTest.java    From spring-kafka with MIT License 6 votes vote down vote up
@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());
  }
}
 
Example #2
Source File: SpringKafkaReceiverTest.java    From spring-kafka with MIT License 6 votes vote down vote up
@Before
public void setUp() throws Exception {
  // set up the Kafka producer properties
  Map<String, Object> senderProperties =
      KafkaTestUtils.senderProps(
          embeddedKafka.getEmbeddedKafka().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.getEmbeddedKafka().getPartitionsPerTopic());
  }
}
 
Example #3
Source File: KafkaAutoConfigurationTest.java    From java-spring-cloud with Apache License 2.0 5 votes vote down vote up
@Bean
public ProducerFactory<String, String> producerFactory() {
  return new ProducerFactory<String, String>() {
    @Override
    public Producer<String, String> createProducer() {
      return null;
    }
  };
}
 
Example #4
Source File: TracingProducerFactory.java    From java-kafka-client with Apache License 2.0 5 votes vote down vote up
public TracingProducerFactory(ProducerFactory<K, V> producerFactory, Tracer tracer,
    Collection<SpanDecorator> spanDecorators,
    BiFunction<String, ProducerRecord, String> producerSpanNameProvider) {
  this.producerFactory = producerFactory;
  this.tracer = tracer;
  this.spanDecorators = (spanDecorators == null)
      ? Collections.singletonList(STANDARD_TAGS)
      : spanDecorators;
  this.producerSpanNameProvider = (producerSpanNameProvider == null)
      ? ClientSpanNameProvider.PRODUCER_OPERATION_NAME
      : producerSpanNameProvider;
}
 
Example #5
Source File: KafkaProducerConfig.java    From stateful-functions with Apache License 2.0 5 votes vote down vote up
@Bean
public ProducerFactory<Object, Object> producerFactory() {
  Map<String, Object> props = new HashMap<>();
  props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
  props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, ByteArraySerializer.class);
  props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, ByteArraySerializer.class);
  props.put("batch.size", 16384);
  props.put("linger.ms", 1);
  props.put("buffer.memory", 33554432);

  return new DefaultKafkaProducerFactory<>(props);
}
 
Example #6
Source File: EventRegistryKafkaConfiguration.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Bean
public ProducerFactory<?, ?> kafkaProducerFactory(KafkaContainer kafkaContainer) {
    Map<String, Object> producerProperties = new HashMap<>();
    producerProperties.put(CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG, kafkaContainer.getBootstrapServers());
    producerProperties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    producerProperties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);

    return new DefaultKafkaProducerFactory<>(producerProperties);
}
 
Example #7
Source File: CommonConfiguration.java    From grussell-spring-kafka with Apache License 2.0 5 votes vote down vote up
@Bean
public ProducerFactory<String, String> producerFactory() {
	Map<String, Object> props = new HashMap<>();
	props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, this.configProperties.getBrokerAddress());
	props.put(ProducerConfig.RETRIES_CONFIG, 0);
	props.put(ProducerConfig.BATCH_SIZE_CONFIG, 16384);
	props.put(ProducerConfig.LINGER_MS_CONFIG, 1);
	props.put(ProducerConfig.BUFFER_MEMORY_CONFIG, 33554432);
	props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
	props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
	return new DefaultKafkaProducerFactory<>(props);
}
 
Example #8
Source File: JsonConfiguration.java    From grussell-spring-kafka with Apache License 2.0 5 votes vote down vote up
@Bean
public ProducerFactory<String, Foo> producerFactory() {
	Map<String, Object> props = new HashMap<>();
	props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, this.configProperties.getBrokerAddress());
	props.put(ProducerConfig.RETRIES_CONFIG, 0);
	props.put(ProducerConfig.BATCH_SIZE_CONFIG, 16384);
	props.put(ProducerConfig.LINGER_MS_CONFIG, 1);
	props.put(ProducerConfig.BUFFER_MEMORY_CONFIG, 33554432);
	props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
	props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class);
	return new DefaultKafkaProducerFactory<>(props);
}
 
Example #9
Source File: KafkaMessageBrokerConfiguration.java    From piper with Apache License 2.0 5 votes vote down vote up
@Bean
public ProducerFactory<String, Object> producerFactory(ObjectMapper aObjectMapper, KafkaProperties aKafkaProperties) {
  return new DefaultKafkaProducerFactory<>(
      producerConfigs(aKafkaProperties),
      new StringSerializer(),
      new JsonSerializer<>(aObjectMapper));
}
 
Example #10
Source File: KafkaEventConfig.java    From enode with MIT License 5 votes vote down vote up
/**
 * 根据senderProps填写的参数创建生产者工厂
 */
@Bean
public ProducerFactory<Object, Object> producerFactory() {
    Map<String, Object> props = new HashMap<>();
    props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, QueueProperties.KAFKA_SERVER);
    props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    return new DefaultKafkaProducerFactory<>(props);
}
 
Example #11
Source File: KafkaCommandConfig.java    From enode with MIT License 5 votes vote down vote up
/**
 * 根据senderProps填写的参数创建生产者工厂
 */
@Bean
public ProducerFactory producerFactory() {
    Map<String, Object> props = new HashMap<>();
    props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, KAFKA_SERVER);
    props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    return new DefaultKafkaProducerFactory<>(props);
}
 
Example #12
Source File: KafkaConfig.java    From Mastering-Distributed-Tracing with MIT License 5 votes vote down vote up
private ProducerFactory<String, Message> producerFactory() throws Exception {
    Map<String, Object> props = new HashMap<>();
    props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
    props.put(ProducerConfig.CLIENT_ID_CONFIG, clientId());
    ProducerFactory<String, Message> producer = //
            new DefaultKafkaProducerFactory<String, Message>(props, //
                    new StringSerializer(), //
                    new JsonSerializer<Message>());
    return new TracingProducerFactory<String, Message>(producer, tracer);
}
 
Example #13
Source File: EnodeTestKafkaConfig.java    From enode with MIT License 5 votes vote down vote up
/**
 * 根据senderProps填写的参数创建生产者工厂
 */
@Bean
public ProducerFactory<String, String> producerFactory() {
    Map<String, Object> props = new HashMap<>();
    props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, Constants.KAFKA_SERVER);
    props.put(ProducerConfig.RETRIES_CONFIG, 1);
    props.put(ProducerConfig.BATCH_SIZE_CONFIG, 16384);
    props.put(ProducerConfig.LINGER_MS_CONFIG, 1);
    props.put(ProducerConfig.BUFFER_MEMORY_CONFIG, 1024000);
    props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    return new DefaultKafkaProducerFactory<>(props);
}
 
Example #14
Source File: KafkaProducerConfig.java    From SpringAll with MIT License 5 votes vote down vote up
@Bean
public ProducerFactory<String, Message> producerFactory() {
    Map<String, Object> configProps = new HashMap<>();
    configProps.put(
            ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,
            bootstrapServers);
    configProps.put(
            ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,
            StringSerializer.class);
    configProps.put(
            ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,
            JsonSerializer.class);
    return new DefaultKafkaProducerFactory<>(configProps);
}
 
Example #15
Source File: KafkaConfiguration.java    From spring-examples with GNU General Public License v3.0 5 votes vote down vote up
@Bean
public ProducerFactory producerFactory() {
    Map<String, Object> config = new HashMap<>();
    config.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaAddress);
    config.put(JsonSerializer.ADD_TYPE_INFO_HEADERS, false);
    config.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    config.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class);
    return new DefaultKafkaProducerFactory(config);
}
 
Example #16
Source File: KafkaConfig.java    From enode with MIT License 5 votes vote down vote up
/**
 * 根据senderProps的参数创建生产者工厂
 */
@Bean
public ProducerFactory producerFactory() {
    Map<String, Object> props = new HashMap<>();
    props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, QueueProperties.KAFKA_SERVER);
    props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    return new DefaultKafkaProducerFactory<>(props);
}
 
Example #17
Source File: ProducerOnlyTransactionTests.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 5 votes vote down vote up
@Bean
public PlatformTransactionManager transactionManager(BinderFactory binders) {
	try {
		ProducerFactory<byte[], byte[]> pf = ((KafkaMessageChannelBinder) binders.getBinder(null,
				MessageChannel.class)).getTransactionalProducerFactory();
		KafkaTransactionManager<byte[], byte[]> tm = new KafkaTransactionManager<>(pf);
		tm.setTransactionSynchronization(AbstractPlatformTransactionManager.SYNCHRONIZATION_ON_ACTUAL_TRANSACTION);
		return tm;
	}
	catch (BeanCreationException e) { // needed to avoid other tests in this package failing when there is no binder
		return null;
	}
}
 
Example #18
Source File: KafkaBinderConfiguration.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean(name = "binderClientFactoryCustomizer")
public ClientFactoryCustomizer binderClientFactoryCustomizer(ConfigurableApplicationContext context) {


	return new ClientFactoryCustomizer() {

		MeterRegistry meterRegistry = context.getBean("outerContext", ApplicationContext.class)
				.getBean(MeterRegistry.class);

		@Override
		public void configure(ProducerFactory<?, ?> pf) {
			if (pf instanceof DefaultKafkaProducerFactory) {
				((DefaultKafkaProducerFactory<?, ?>) pf)
						.addListener(new MicrometerProducerListener<>(this.meterRegistry));
			}
		}

		@Override
		public void configure(ConsumerFactory<?, ?> cf) {
			if (cf instanceof DefaultKafkaConsumerFactory) {
				((DefaultKafkaConsumerFactory<?, ?>) cf)
						.addListener(new MicrometerConsumerListener<>(this.meterRegistry));
			}
		}

	};

}
 
Example #19
Source File: SpringBootDecisionTracingConfiguration.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
/**
 * Configure producers
 */
@Bean
public ProducerFactory<String, String> producerFactory() {
    Map<String, Object> configProps = new HashMap<>();
    configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaBootstrapAddress);
    configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    return new DefaultKafkaProducerFactory<>(configProps);
}
 
Example #20
Source File: ReplyConfiguration.java    From faster-framework-project with Apache License 2.0 5 votes vote down vote up
public ReplyConfiguration(KafkaProperties properties,
                          org.springframework.boot.autoconfigure.kafka.KafkaProperties kafkaProperties,
                          ObjectProvider<RecordMessageConverter> messageConverter,
                          ConsumerFactory<Object, Object> consumerFactory,
                          ProducerFactory<Object, Object> producerFactory,
                          ProducerListener<Object, Object> producerListener) {
    this.kafkaProperties = kafkaProperties;
    this.properties = properties;
    this.messageConverter = messageConverter.getIfUnique();
    this.consumerFactory = consumerFactory;
    this.producerFactory = producerFactory;
    this.producerListener = producerListener;
}
 
Example #21
Source File: SpringKafkaTest.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
private KafkaTemplate<Integer, String> createTemplate() {
    Map<String, Object> senderProps = senderProps();
    ProducerFactory<Integer, String> pf =
        new DefaultKafkaProducerFactory<>(senderProps);
    KafkaTemplate<Integer, String> template = new KafkaTemplate<>(pf);
    return template;
}
 
Example #22
Source File: KafkaProducerConfig.java    From personal_book_library_web_project with MIT License 5 votes vote down vote up
@Bean
public ProducerFactory<String, MailContext> mailContextProducerFactory() {
    Map<String, Object> configProps = new HashMap<>();
    configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, environment.getProperty("kafka.bootstrap.address"));
    configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class);
    configProps.put(ProducerConfig.MAX_BLOCK_MS_CONFIG, 5000);
    return new DefaultKafkaProducerFactory<>(configProps);
}
 
Example #23
Source File: KafkaProducersConfig.java    From springboot_cwenao with MIT License 5 votes vote down vote up
public ProducerFactory<String, String> producerFactory() {

        // set the producer properties
        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, brokers);
        properties.put(ProducerConfig.RETRIES_CONFIG, 0);
        properties.put(ProducerConfig.BATCH_SIZE_CONFIG, 4096);
        properties.put(ProducerConfig.LINGER_MS_CONFIG, 1);
        properties.put(ProducerConfig.BUFFER_MEMORY_CONFIG, 40960);
        properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);

        return new DefaultKafkaProducerFactory<String, String>(properties);
    }
 
Example #24
Source File: NotificationEventConfig.java    From stream-registry with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnProperty(name = KAFKA_NOTIFICATIONS_ENABLED_PROPERTY)
public ProducerFactory<SpecificRecord, SpecificRecord> producerFactory() {
  log.info("Building kafka producer in cluster {} with schema registry {}", bootstrapServers, schemaRegistryUrl);
  Objects.requireNonNull(bootstrapServers, getWarningMessageOnNotDefinedProp("enabled notification events", KAFKA_BOOTSTRAP_SERVERS_PROPERTY));
  Objects.requireNonNull(schemaRegistryUrl, getWarningMessageOnNotDefinedProp("enabled notification events", KAFKA_SCHEMA_REGISTRY_URL_PROPERTY));

  val props = new HashMap<String, Object>();
  props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
  props.put(KafkaAvroSerializerConfig.SCHEMA_REGISTRY_URL_CONFIG, schemaRegistryUrl);
  props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, KafkaAvroSerializer.class);
  props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, KafkaAvroSerializer.class);

  return new DefaultKafkaProducerFactory<>(props);
}
 
Example #25
Source File: KafkaAutoConfigurationTest.java    From java-spring-cloud with Apache License 2.0 5 votes vote down vote up
@Test
public void loadKafkaTracingProducerFactory() {
  AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
  context.register(TracerConfig.class, FakeKafkaConfig.class, KafkaAutoConfiguration.class);
  context.refresh();
  ProducerFactory tracingProducerFactory = context.getBean(ProducerFactory.class);
  assertTrue(tracingProducerFactory instanceof TracingProducerFactory);
}
 
Example #26
Source File: KafkaMessageLogReceiverEndpointIntegrationTest.java    From synapse with Apache License 2.0 5 votes vote down vote up
@Bean
public ProducerFactory<String, String> producerFactory(final EmbeddedKafkaBroker embeddedKafkaBroker) {
    final Map<String, Object> configs = producerProps(embeddedKafkaBroker);
    configs.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    configs.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    return new DefaultKafkaProducerFactory<>(configs);
}
 
Example #27
Source File: KafkaAutoConfigurationTest.java    From java-spring-cloud with Apache License 2.0 5 votes vote down vote up
@Test
public void loadNormalProducerFactoryWhenDisabled() {
  AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
  context.register(TracerConfig.class, FakeKafkaConfig.class, KafkaAutoConfiguration.class);
  TestPropertyValues.of("opentracing.spring.cloud.kafka.enabled:false").applyTo(context);
  context.refresh();
  ProducerFactory tracingProducerFactory = context.getBean(ProducerFactory.class);
  assertFalse(tracingProducerFactory instanceof TracingProducerFactory);
}
 
Example #28
Source File: NotificationEventListenerKafkaIntegrationTest.java    From stream-registry with Apache License 2.0 5 votes vote down vote up
public ProducerFactory<SpecificRecord, SpecificRecord> producerFactory() {
  Objects.requireNonNull(bootstrapServers, getWarningMessageOnNotDefinedProp("enabled notification events", KAFKA_BOOTSTRAP_SERVERS_PROPERTY));

  Map<String, Object> props = new HashMap<>();
  props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
  props.put(KafkaAvroSerializerConfig.SCHEMA_REGISTRY_URL_CONFIG, TEST_SCHEMA_REGISTRY);
  props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, TestSerializer.class);
  props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, TestSerializer.class);

  return new DefaultKafkaProducerFactory<>(props);
}
 
Example #29
Source File: ConsumerProducerTransactionTests.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
@Bean
public KafkaAwareTransactionManager<byte[], byte[]> tm(ProducerFactory pf) {
	KafkaAwareTransactionManager mock = mock(KafkaAwareTransactionManager.class);
	this.pf = pf;
	given(mock.getProducerFactory()).willReturn(pf);
	this.tm = mock;
	return mock;
}
 
Example #30
Source File: TracingProducerFactory.java    From java-kafka-client with Apache License 2.0 4 votes vote down vote up
public TracingProducerFactory(ProducerFactory<K, V> producerFactory, Tracer tracer,
    Collection<SpanDecorator> spanDecorators) {
  this(producerFactory, tracer, spanDecorators, null);
}