当前位置: 首页>>代码示例>>Java>>正文


Java ProducerConfiguration.setSendTimeout方法代码示例

本文整理汇总了Java中org.apache.pulsar.client.api.ProducerConfiguration.setSendTimeout方法的典型用法代码示例。如果您正苦于以下问题:Java ProducerConfiguration.setSendTimeout方法的具体用法?Java ProducerConfiguration.setSendTimeout怎么用?Java ProducerConfiguration.setSendTimeout使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.pulsar.client.api.ProducerConfiguration的用法示例。


在下文中一共展示了ProducerConfiguration.setSendTimeout方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testAheadProducerOnHold

import org.apache.pulsar.client.api.ProducerConfiguration; //导入方法依赖的package包/类
@Test
public void testAheadProducerOnHold() throws Exception {
    assertEquals(admin.namespaces().getBacklogQuotaMap("prop/usc/quotahold"), Maps.newTreeMap());
    admin.namespaces().setBacklogQuota("prop/usc/quotahold",
            new BacklogQuota(10 * 1024, BacklogQuota.RetentionPolicy.producer_request_hold));
    final ClientConfiguration clientConf = new ClientConfiguration();
    clientConf.setStatsInterval(0, TimeUnit.SECONDS);
    final PulsarClient client = PulsarClient.create(adminUrl.toString(), clientConf);
    final String topic1 = "persistent://prop/usc/quotahold/hold";
    final String subName1 = "c1hold";
    final int numMsgs = 10;

    Consumer consumer = client.subscribe(topic1, subName1);
    ProducerConfiguration producerConfiguration = new ProducerConfiguration();
    producerConfiguration.setSendTimeout(2, TimeUnit.SECONDS);

    byte[] content = new byte[1024];
    Producer producer = client.createProducer(topic1, producerConfiguration);
    for (int i = 0; i <= numMsgs; i++) {
        try {
            producer.send(content);
            LOG.info("sent [{}]", i);
        } catch (PulsarClientException.TimeoutException cte) {
            // producer close may cause a timeout on send
            LOG.info("timeout on [{}]", i);
        }
    }

    for (int i = 0; i < numMsgs; i++) {
        consumer.receive();
        LOG.info("received [{}]", i);
    }

    Thread.sleep((TIME_TO_CHECK_BACKLOG_QUOTA + 1) * 1000);
    rolloverStats();
    PersistentTopicStats stats = admin.persistentTopics().getStats(topic1);
    Assert.assertEquals(stats.publishers.size(), 0,
            "Number of producers on topic " + topic1 + " are [" + stats.publishers.size() + "]");
    client.close();
}
 
开发者ID:apache,项目名称:incubator-pulsar,代码行数:41,代码来源:BacklogQuotaManagerTest.java

示例2: testAheadProducerOnHoldTimeout

import org.apache.pulsar.client.api.ProducerConfiguration; //导入方法依赖的package包/类
@Test
public void testAheadProducerOnHoldTimeout() throws Exception {
    assertEquals(admin.namespaces().getBacklogQuotaMap("prop/usc/quotahold"), Maps.newTreeMap());
    admin.namespaces().setBacklogQuota("prop/usc/quotahold",
            new BacklogQuota(10 * 1024, BacklogQuota.RetentionPolicy.producer_request_hold));
    final ClientConfiguration clientConf = new ClientConfiguration();
    clientConf.setStatsInterval(0, TimeUnit.SECONDS);
    final PulsarClient client = PulsarClient.create(adminUrl.toString(), clientConf);
    final String topic1 = "persistent://prop/usc/quotahold/holdtimeout";
    final String subName1 = "c1holdtimeout";
    boolean gotException = false;

    client.subscribe(topic1, subName1);
    ProducerConfiguration producerConfiguration = new ProducerConfiguration();
    producerConfiguration.setSendTimeout(2, TimeUnit.SECONDS);

    byte[] content = new byte[1024];
    Producer producer = client.createProducer(topic1, producerConfiguration);
    for (int i = 0; i < 10; i++) {
        producer.send(content);
    }

    Thread.sleep((TIME_TO_CHECK_BACKLOG_QUOTA + 1) * 1000);

    try {
        // try to send over backlog quota and make sure it fails
        producer.send(content);
        producer.send(content);
        Assert.fail("backlog quota did not exceed");
    } catch (PulsarClientException.TimeoutException te) {
        gotException = true;
    }

    Assert.assertTrue(gotException, "timeout did not occur");
    client.close();
}
 
开发者ID:apache,项目名称:incubator-pulsar,代码行数:37,代码来源:BacklogQuotaManagerTest.java

示例3: testProducerException

import org.apache.pulsar.client.api.ProducerConfiguration; //导入方法依赖的package包/类
@Test
public void testProducerException() throws Exception {
    assertEquals(admin.namespaces().getBacklogQuotaMap("prop/usc/quotahold"), Maps.newTreeMap());
    admin.namespaces().setBacklogQuota("prop/usc/quotahold",
            new BacklogQuota(10 * 1024, BacklogQuota.RetentionPolicy.producer_exception));
    final ClientConfiguration clientConf = new ClientConfiguration();
    clientConf.setStatsInterval(0, TimeUnit.SECONDS);
    final PulsarClient client = PulsarClient.create(adminUrl.toString(), clientConf);
    final String topic1 = "persistent://prop/usc/quotahold/except";
    final String subName1 = "c1except";
    boolean gotException = false;

    client.subscribe(topic1, subName1);
    ProducerConfiguration producerConfiguration = new ProducerConfiguration();
    producerConfiguration.setSendTimeout(2, TimeUnit.SECONDS);

    byte[] content = new byte[1024];
    Producer producer = client.createProducer(topic1, producerConfiguration);
    for (int i = 0; i < 10; i++) {
        producer.send(content);
    }

    Thread.sleep((TIME_TO_CHECK_BACKLOG_QUOTA + 1) * 1000);

    try {
        // try to send over backlog quota and make sure it fails
        producer.send(content);
        producer.send(content);
        Assert.fail("backlog quota did not exceed");
    } catch (PulsarClientException ce) {
        Assert.assertTrue(ce instanceof PulsarClientException.ProducerBlockedQuotaExceededException
                || ce instanceof PulsarClientException.TimeoutException, ce.getMessage());
        gotException = true;
    }

    Assert.assertTrue(gotException, "backlog exceeded exception did not occur");
    client.close();
}
 
开发者ID:apache,项目名称:incubator-pulsar,代码行数:39,代码来源:BacklogQuotaManagerTest.java

示例4: LoadSimulationClient

import org.apache.pulsar.client.api.ProducerConfiguration; //导入方法依赖的package包/类
/**
 * Create a LoadSimulationClient with the given JCommander arguments.
 *
 * @param arguments
 *            Arguments to configure this from.
 */
public LoadSimulationClient(final MainArguments arguments) throws Exception {
    payloadCache = new ConcurrentHashMap<>();
    topicsToTradeUnits = new ConcurrentHashMap<>();

    clientConf = new ClientConfiguration();

    clientConf.setConnectionsPerBroker(4);
    clientConf.setIoThreads(Runtime.getRuntime().availableProcessors());

    // Disable stats on the clients to reduce CPU/memory usage.
    clientConf.setStatsInterval(0, TimeUnit.SECONDS);

    producerConf = new ProducerConfiguration();

    // Disable timeout.
    producerConf.setSendTimeout(0, TimeUnit.SECONDS);

    producerConf.setMessageRoutingMode(ProducerConfiguration.MessageRoutingMode.RoundRobinPartition);

    // Enable batching.
    producerConf.setBatchingMaxPublishDelay(1, TimeUnit.MILLISECONDS);
    producerConf.setBatchingEnabled(true);
    consumerConf = new ConsumerConfiguration();
    consumerConf.setMessageListener(ackListener);
    admin = new PulsarAdmin(new URL(arguments.serviceURL), clientConf);
    client = new PulsarClientImpl(arguments.serviceURL, clientConf);
    port = arguments.port;
    executor = Executors.newCachedThreadPool(new DefaultThreadFactory("test-client"));
}
 
开发者ID:apache,项目名称:incubator-pulsar,代码行数:36,代码来源:LoadSimulationClient.java

示例5: PulsarKafkaProducer

import org.apache.pulsar.client.api.ProducerConfiguration; //导入方法依赖的package包/类
@SuppressWarnings({ "unchecked", "deprecation" })
private PulsarKafkaProducer(Map<String, Object> conf, Properties properties, Serializer<K> keySerializer,
        Serializer<V> valueSerializer) {
    properties.forEach((k, v) -> conf.put((String) k, v));

    ProducerConfig producerConfig = new ProducerConfig(conf);

    if (keySerializer == null) {
        this.keySerializer = producerConfig.getConfiguredInstance(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,
                Serializer.class);
        this.keySerializer.configure(producerConfig.originals(), true);
    } else {
        this.keySerializer = keySerializer;
        producerConfig.ignore(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG);
    }

    if (valueSerializer == null) {
        this.valueSerializer = producerConfig.getConfiguredInstance(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,
                Serializer.class);
        this.valueSerializer.configure(producerConfig.originals(), true);
    } else {
        this.valueSerializer = valueSerializer;
        producerConfig.ignore(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG);
    }

    String serviceUrl = producerConfig.getList(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG).get(0);
    ClientConfiguration clientConf = PulsarKafkaConfig.getClientConfiguration(properties);
    try {
        client = PulsarClient.create(serviceUrl, clientConf);
    } catch (PulsarClientException e) {
        throw new RuntimeException(e);
    }

    pulsarProducerConf = new ProducerConfiguration();
    pulsarProducerConf.setBatchingEnabled(true);

    // To mimic the same batching mode as Kafka, we need to wait a very little amount of
    // time to batch if the client is trying to send messages fast enough
    long lingerMs = Long.parseLong(properties.getProperty(ProducerConfig.LINGER_MS_CONFIG, "1"));
    pulsarProducerConf.setBatchingMaxPublishDelay(lingerMs, TimeUnit.MILLISECONDS);

    String compressionType = properties.getProperty(ProducerConfig.COMPRESSION_TYPE_CONFIG);
    if ("gzip".equals(compressionType)) {
        pulsarProducerConf.setCompressionType(CompressionType.ZLIB);
    } else if ("lz4".equals(compressionType)) {
        pulsarProducerConf.setCompressionType(CompressionType.LZ4);
    }

    pulsarProducerConf.setSendTimeout(
            Integer.parseInt(properties.getProperty(ProducerConfig.MAX_BLOCK_MS_CONFIG, "60000")),
            TimeUnit.MILLISECONDS);

    boolean blockOnBufferFull = Boolean
            .parseBoolean(properties.getProperty(ProducerConfig.BLOCK_ON_BUFFER_FULL_CONFIG, "false"));

    // Kafka blocking semantic when blockOnBufferFull=false is different from Pulsar client
    // Pulsar throws error immediately when the queue is full and blockIfQueueFull=false
    // Kafka, on the other hand, still blocks for "max.block.ms" time and then gives error.
    boolean shouldBlockPulsarProducer = pulsarProducerConf.getSendTimeoutMs() > 0 || blockOnBufferFull;
    pulsarProducerConf.setBlockIfQueueFull(shouldBlockPulsarProducer);
}
 
开发者ID:apache,项目名称:incubator-pulsar,代码行数:62,代码来源:PulsarKafkaProducer.java

示例6: getProducerConfiguration

import org.apache.pulsar.client.api.ProducerConfiguration; //导入方法依赖的package包/类
private ProducerConfiguration getProducerConfiguration() {
    ProducerConfiguration conf = new ProducerConfiguration();

    // Set to false to prevent the server thread from being blocked if a lot of messages are pending.
    conf.setBlockIfQueueFull(false);

    if (queryParams.containsKey("sendTimeoutMillis")) {
        conf.setSendTimeout(Integer.parseInt(queryParams.get("sendTimeoutMillis")), TimeUnit.MILLISECONDS);
    }

    if (queryParams.containsKey("batchingEnabled")) {
        conf.setBatchingEnabled(Boolean.parseBoolean(queryParams.get("batchingEnabled")));
    }

    if (queryParams.containsKey("batchingMaxMessages")) {
        conf.setBatchingMaxMessages(Integer.parseInt(queryParams.get("batchingMaxMessages")));
    }

    if (queryParams.containsKey("maxPendingMessages")) {
        conf.setMaxPendingMessages(Integer.parseInt(queryParams.get("maxPendingMessages")));
    }

    if (queryParams.containsKey("batchingMaxPublishDelay")) {
        conf.setBatchingMaxPublishDelay(Integer.parseInt(queryParams.get("batchingMaxPublishDelay")),
                TimeUnit.MILLISECONDS);
    }

    if (queryParams.containsKey("messageRoutingMode")) {
        checkArgument(
                Enums.getIfPresent(MessageRoutingMode.class, queryParams.get("messageRoutingMode")).isPresent(),
                "Invalid messageRoutingMode %s", queryParams.get("messageRoutingMode"));
        MessageRoutingMode routingMode = MessageRoutingMode.valueOf(queryParams.get("messageRoutingMode"));
        if (!MessageRoutingMode.CustomPartition.equals(routingMode)) {
            conf.setMessageRoutingMode(routingMode);
        }
    }

    if (queryParams.containsKey("compressionType")) {
        checkArgument(Enums.getIfPresent(CompressionType.class, queryParams.get("compressionType")).isPresent(),
                "Invalid compressionType %s", queryParams.get("compressionType"));
        conf.setCompressionType(CompressionType.valueOf(queryParams.get("compressionType")));
    }

    return conf;
}
 
开发者ID:apache,项目名称:incubator-pulsar,代码行数:46,代码来源:ProducerHandler.java

示例7: testSendTimeout

import org.apache.pulsar.client.api.ProducerConfiguration; //导入方法依赖的package包/类
@Test(dataProvider = "batch")
public void testSendTimeout(int batchMessageDelayMs) throws Exception {
    log.info("-- Starting {} test --", methodName);

    ConsumerConfiguration consumerConf = new ConsumerConfiguration();
    consumerConf.setSubscriptionType(SubscriptionType.Exclusive);
    Consumer consumer = pulsarClient.subscribe("persistent://my-property/tp1/my-ns/my-topic5", "my-subscriber-name",
            consumerConf);
    ProducerConfiguration producerConf = new ProducerConfiguration();
    if (batchMessageDelayMs != 0) {
        producerConf.setBatchingMaxPublishDelay(2 * batchMessageDelayMs, TimeUnit.MILLISECONDS);
        producerConf.setBatchingMaxMessages(5);
        producerConf.setBatchingEnabled(true);
    }
    producerConf.setSendTimeout(1, TimeUnit.SECONDS);

    Producer producer = pulsarClient.createProducer("persistent://my-property/tp1/my-ns/my-topic5", producerConf);
    final String message = "my-message";

    // Trigger the send timeout
    stopBroker();

    Future<MessageId> future = producer.sendAsync(message.getBytes());

    try {
        future.get();
        fail("Send operation should have failed");
    } catch (ExecutionException e) {
        // Expected
    }

    startBroker();

    // We should not have received any message
    Message msg = consumer.receive(3, TimeUnit.SECONDS);
    assertNull(msg);
    consumer.close();
    producer.close();
    Thread.sleep(1000);
    ConsumerStats cStat = consumer.getStats();
    ProducerStats pStat = producer.getStats();
    assertEquals(pStat.getTotalMsgsSent(), 0);
    assertEquals(pStat.getTotalSendFailed(), 1);
    assertEquals(cStat.getTotalMsgsReceived(), 0);
    assertEquals(cStat.getTotalMsgsReceived(), cStat.getTotalAcksSent());
    log.info("-- Exiting {} test --", methodName);
}
 
开发者ID:apache,项目名称:incubator-pulsar,代码行数:48,代码来源:SimpleProducerConsumerStatTest.java

示例8: testProducerExceptionAndThenUnblock

import org.apache.pulsar.client.api.ProducerConfiguration; //导入方法依赖的package包/类
@Test
public void testProducerExceptionAndThenUnblock() throws Exception {
    assertEquals(admin.namespaces().getBacklogQuotaMap("prop/usc/quotahold"), Maps.newTreeMap());
    admin.namespaces().setBacklogQuota("prop/usc/quotahold",
            new BacklogQuota(10 * 1024, BacklogQuota.RetentionPolicy.producer_exception));
    final ClientConfiguration clientConf = new ClientConfiguration();
    clientConf.setStatsInterval(0, TimeUnit.SECONDS);
    final PulsarClient client = PulsarClient.create(adminUrl.toString(), clientConf);
    final String topic1 = "persistent://prop/usc/quotahold/exceptandunblock";
    final String subName1 = "c1except";
    boolean gotException = false;

    Consumer consumer = client.subscribe(topic1, subName1);
    ProducerConfiguration producerConfiguration = new ProducerConfiguration();
    producerConfiguration.setSendTimeout(2, TimeUnit.SECONDS);

    byte[] content = new byte[1024];
    Producer producer = client.createProducer(topic1, producerConfiguration);
    for (int i = 0; i < 10; i++) {
        producer.send(content);
    }

    Thread.sleep((TIME_TO_CHECK_BACKLOG_QUOTA + 1) * 1000);

    try {
        // try to send over backlog quota and make sure it fails
        producer.send(content);
        producer.send(content);
        Assert.fail("backlog quota did not exceed");
    } catch (PulsarClientException ce) {
        Assert.assertTrue(ce instanceof PulsarClientException.ProducerBlockedQuotaExceededException
                || ce instanceof PulsarClientException.TimeoutException, ce.getMessage());
        gotException = true;
    }

    Assert.assertTrue(gotException, "backlog exceeded exception did not occur");
    // now remove backlog and ensure that producer is unblockedrolloverStats();

    PersistentTopicStats stats = admin.persistentTopics().getStats(topic1);
    int backlog = (int) stats.subscriptions.get(subName1).msgBacklog;

    for (int i = 0; i < backlog; i++) {
        Message msg = consumer.receive();
        consumer.acknowledge(msg);
    }
    Thread.sleep((TIME_TO_CHECK_BACKLOG_QUOTA + 1) * 1000);
    // publish should work now
    Exception sendException = null;
    gotException = false;
    try {
        for (int i = 0; i < 5; i++) {
            producer.send(content);
        }
    } catch (Exception e) {
        gotException = true;
        sendException = e;
    }
    Assert.assertFalse(gotException, "unable to publish due to " + sendException);
    client.close();
}
 
开发者ID:apache,项目名称:incubator-pulsar,代码行数:61,代码来源:BacklogQuotaManagerTest.java


注:本文中的org.apache.pulsar.client.api.ProducerConfiguration.setSendTimeout方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。