本文整理汇总了Java中org.apache.pulsar.client.api.ProducerConfiguration类的典型用法代码示例。如果您正苦于以下问题:Java ProducerConfiguration类的具体用法?Java ProducerConfiguration怎么用?Java ProducerConfiguration使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ProducerConfiguration类属于org.apache.pulsar.client.api包,在下文中一共展示了ProducerConfiguration类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: AbstractReplicator
import org.apache.pulsar.client.api.ProducerConfiguration; //导入依赖的package包/类
public AbstractReplicator(String topicName, String replicatorPrefix, String localCluster,
String remoteCluster, BrokerService brokerService) {
this.brokerService = brokerService;
this.topicName = topicName;
this.replicatorPrefix = replicatorPrefix;
this.localCluster = localCluster.intern();
this.remoteCluster = remoteCluster.intern();
this.client = (PulsarClientImpl) brokerService.getReplicationClient(remoteCluster);
this.producer = null;
this.producerQueueSize = brokerService.pulsar().getConfiguration().getReplicationProducerQueueSize();
this.producerConfiguration = new ProducerConfiguration();
this.producerConfiguration.setSendTimeout(0, TimeUnit.SECONDS);
this.producerConfiguration.setMaxPendingMessages(producerQueueSize);
this.producerConfiguration.setProducerName(getReplicatorName(replicatorPrefix, localCluster));
STATE_UPDATER.set(this, State.Stopped);
}
示例2: testPartitionedTopicNameWithSpecialCharacter
import org.apache.pulsar.client.api.ProducerConfiguration; //导入依赖的package包/类
@Test(timeOut = 30000)
public void testPartitionedTopicNameWithSpecialCharacter() throws Exception {
log.info("-- Starting {} test --", methodName);
int numPartitions = 4;
final String specialCharacter = "! * ' ( ) ; : @ & = + $ , /\\ ? % # [ ]";
final String topicName = "my-partitionedtopic1" + specialCharacter;
DestinationName dn = DestinationName.get("persistent://my-property/use/my-ns/" + topicName);
admin.persistentTopics().createPartitionedTopic(dn.toString(), numPartitions);
ProducerConfiguration producerConf = new ProducerConfiguration();
producerConf.setMessageRoutingMode(MessageRoutingMode.RoundRobinPartition);
// Try to create producer which does lookup and create connection with broker
Producer producer = pulsarClient.createProducer(dn.toString(), producerConf);
producer.close();
admin.persistentTopics().deletePartitionedTopic(dn.toString());
log.info("-- Exiting {} test --", methodName);
}
示例3: publishMessagesBase
import org.apache.pulsar.client.api.ProducerConfiguration; //导入依赖的package包/类
private Set<String> publishMessagesBase(String topic, int count, boolean batching) throws Exception {
Set<String> keys = new HashSet<>();
ProducerConfiguration producerConf = new ProducerConfiguration();
producerConf.setMaxPendingMessages(count);
producerConf.setBatchingEnabled(batching);
producerConf.setBatchingMaxMessages(BATCH_MAX_MESSAGES);
producerConf.setBatchingMaxPublishDelay(Long.MAX_VALUE, TimeUnit.DAYS);
try (Producer producer = pulsarClient.createProducer(topic, producerConf)) {
Future<?> lastFuture = null;
for (int i = 0; i < count; i++) {
String key = "key"+i;
byte[] data = ("my-message-" + i).getBytes();
lastFuture = producer.sendAsync(MessageBuilder.create()
.setKey(key)
.setContent(data).build());
keys.add(key);
}
lastFuture.get();
}
return keys;
}
示例4: MessageProducer
import org.apache.pulsar.client.api.ProducerConfiguration; //导入依赖的package包/类
MessageProducer(URL url, final DestinationName dest, boolean batch) throws Exception {
this.url = url;
this.namespace = dest.getNamespace();
this.topicName = dest.toString();
ClientConfiguration conf = new ClientConfiguration();
conf.setStatsInterval(0, TimeUnit.SECONDS);
client = PulsarClient.create(url.toString(), conf);
ProducerConfiguration producerConfiguration = new ProducerConfiguration();
if (batch) {
producerConfiguration.setBatchingEnabled(true);
producerConfiguration.setBatchingMaxPublishDelay(1, TimeUnit.SECONDS);
producerConfiguration.setBatchingMaxMessages(5);
}
producer = client.createProducer(topicName, producerConfiguration);
}
示例5: testPartitions
import org.apache.pulsar.client.api.ProducerConfiguration; //导入依赖的package包/类
@Test
public void testPartitions() throws Exception {
admin.properties().createProperty("sample", new PropertyAdmin());
PulsarClient client = PulsarClient.create("pulsar://localhost:" + proxyConfig.getServicePort());
admin.persistentTopics().createPartitionedTopic("persistent://sample/test/local/partitioned-topic", 2);
ProducerConfiguration producerConf = new ProducerConfiguration();
producerConf.setMessageRoutingMode(MessageRoutingMode.RoundRobinPartition);
Producer producer = client.createProducer("persistent://sample/test/local/partitioned-topic", producerConf);
// Create a consumer directly attached to broker
Consumer consumer = pulsarClient.subscribe("persistent://sample/test/local/partitioned-topic", "my-sub");
for (int i = 0; i < 10; i++) {
producer.send("test".getBytes());
}
for (int i = 0; i < 10; i++) {
Message msg = consumer.receive(1, TimeUnit.SECONDS);
checkNotNull(msg);
}
client.close();
}
示例6: ProducerStats
import org.apache.pulsar.client.api.ProducerConfiguration; //导入依赖的package包/类
public ProducerStats(PulsarClientImpl pulsarClient, ProducerConfiguration conf, ProducerImpl producer) {
this.pulsarClient = pulsarClient;
this.statsIntervalSeconds = pulsarClient.getConfiguration().getStatsIntervalSeconds();
this.producer = producer;
numMsgsSent = new LongAdder();
numBytesSent = new LongAdder();
numSendFailed = new LongAdder();
numAcksReceived = new LongAdder();
totalMsgsSent = new LongAdder();
totalBytesSent = new LongAdder();
totalSendFailed = new LongAdder();
totalAcksReceived = new LongAdder();
ds = DoublesSketch.builder().build(256);
dec = new DecimalFormat("0.000");
throughputFormat = new DecimalFormat("0.00");
init(conf);
}
示例7: TradeUnit
import org.apache.pulsar.client.api.ProducerConfiguration; //导入依赖的package包/类
public TradeUnit(final TradeConfiguration tradeConf, final PulsarClient client,
final ProducerConfiguration producerConf, final ConsumerConfiguration consumerConf,
final Map<Integer, byte[]> payloadCache) throws Exception {
consumerFuture = client.subscribeAsync(tradeConf.topic, "Subscriber-" + tradeConf.topic, consumerConf);
this.payload = new AtomicReference<>();
this.producerConf = producerConf;
this.payloadCache = payloadCache;
this.client = client;
topic = tradeConf.topic;
// Add a byte[] of the appropriate size if it is not already present
// in the cache.
this.payload.set(payloadCache.computeIfAbsent(tradeConf.size, byte[]::new));
rateLimiter = RateLimiter.create(tradeConf.rate);
stop = new AtomicBoolean(false);
}
示例8: main
import org.apache.pulsar.client.api.ProducerConfiguration; //导入依赖的package包/类
public static void main(String[] args) throws IOException {
// Create a Pulsar client instance. A single instance can be shared across many
// producers and consumer within the same application
PulsarClient client = PulsarClient.create(SERVICE_URL);
// Here you get the chance to configure producer specific settings. eg:
ProducerConfiguration conf = new ProducerConfiguration();
// Enable compression
conf.setCompressionType(CompressionType.LZ4);
// Once the producer is created, it can be used for the entire application life-cycle
Producer producer = client.createProducer(TOPIC_NAME, conf);
log.info("Created Pulsar producer");
// Send few test messages
for (int i = 0; i < 10; i++) {
String content = String.format("hello-pulsar-%d", i);
// Build a message object
Message msg = MessageBuilder.create().setContent(content.getBytes()).build();
// Send a message (waits until the message is persisted)
MessageId msgId = producer.send(msg);
log.info("Published msg='{}' with msg-id={}", content, msgId);
}
client.close();
}
示例9: ProducerHandler
import org.apache.pulsar.client.api.ProducerConfiguration; //导入依赖的package包/类
public ProducerHandler(WebSocketService service, HttpServletRequest request, ServletUpgradeResponse response) {
super(service, request, response);
this.numMsgsSent = new LongAdder();
this.numBytesSent = new LongAdder();
this.numMsgsFailed = new LongAdder();
this.publishLatencyStatsUSec = new StatsBuckets(ENTRY_LATENCY_BUCKETS_USEC);
if (!authResult) {
return;
}
try {
ProducerConfiguration conf = getProducerConfiguration();
this.producer = service.getPulsarClient().createProducer(topic, conf);
if (!this.service.addProducer(this)) {
log.warn("[{}:{}] Failed to add producer handler for topic {}", request.getRemoteAddr(),
request.getRemotePort(), topic);
}
} catch (Exception e) {
log.warn("[{}:{}] Failed in creating producer on topic {}", request.getRemoteAddr(),
request.getRemotePort(), topic, e);
boolean configError = e instanceof IllegalArgumentException;
int errorCode = configError ? HttpServletResponse.SC_BAD_REQUEST
: HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
String errorMsg = configError ? "Invalid query-param " + e.getMessage() : "Failed to create producer";
try {
response.sendError(errorCode, errorMsg);
} catch (IOException e1) {
log.warn("[{}:{}] Failed to send error: {}", request.getRemoteAddr(), request.getRemotePort(),
e1.getMessage(), e1);
}
}
}
示例10: testBatchMessagesRateOut
import org.apache.pulsar.client.api.ProducerConfiguration; //导入依赖的package包/类
public void testBatchMessagesRateOut() throws PulsarClientException, InterruptedException, PulsarAdminException {
log.info("-- Starting {} test --", methodName);
String topicName = "persistent://my-property/cluster/my-ns/testBatchMessagesRateOut";
double produceRate = 17;
int batchSize = 5;
ConsumerConfiguration consumerConf = new ConsumerConfiguration();
consumerConf.setSubscriptionType(SubscriptionType.Exclusive);
Consumer consumer = pulsarClient.subscribe(topicName, "my-subscriber-name", consumerConf);
ProducerConfiguration producerConf = new ProducerConfiguration();
producerConf.setBatchingMaxMessages(batchSize);
producerConf.setBatchingEnabled(true);
producerConf.setBatchingMaxPublishDelay(2, TimeUnit.SECONDS);
Producer producer = pulsarClient.createProducer(topicName, producerConf);
AtomicBoolean runTest = new AtomicBoolean(true);
Thread t1 = new Thread(() -> {
RateLimiter r = RateLimiter.create(produceRate);
while (runTest.get()) {
r.acquire();
producer.sendAsync("Hello World".getBytes());
consumer.receiveAsync().thenAccept(message -> consumer.acknowledgeAsync(message));
}
});
t1.start();
Thread.sleep(2000); // Two seconds sleep
runTest.set(false);
pulsar.getBrokerService().updateRates();
double actualRate = admin.persistentTopics().getStats(topicName).msgRateOut;
assertTrue(actualRate > (produceRate / batchSize));
consumer.unsubscribe();
log.info("-- Exiting {} test --", methodName);
}
示例11: testSyncProducerAndConsumer
import org.apache.pulsar.client.api.ProducerConfiguration; //导入依赖的package包/类
public void testSyncProducerAndConsumer(int batchMessageDelayMs) throws Exception {
ConsumerConfiguration conf = new ConsumerConfiguration();
conf.setSubscriptionType(SubscriptionType.Exclusive);
Consumer consumer = pulsarClient.subscribe("persistent://my-property/use/my-ns/my-topic", "my-subscriber-name",
conf);
ProducerConfiguration producerConf = new ProducerConfiguration();
if (batchMessageDelayMs != 0) {
producerConf.setBatchingEnabled(true);
producerConf.setBatchingMaxPublishDelay(batchMessageDelayMs, TimeUnit.MILLISECONDS);
producerConf.setBatchingMaxMessages(5);
}
Producer producer = pulsarClient.createProducer("persistent://my-property/use/my-ns/my-topic", producerConf);
for (int i = 0; i < 10; i++) {
String message = "my-message-" + i;
producer.send(message.getBytes());
}
Message msg = null;
Set<String> messageSet = Sets.newHashSet();
for (int i = 0; i < 10; i++) {
msg = consumer.receive(5, TimeUnit.SECONDS);
String receivedMessage = new String(msg.getData());
log.debug("Received message: [{}]", receivedMessage);
String expectedMessage = "my-message-" + i;
testMessageOrderAndDuplicates(messageSet, receivedMessage, expectedMessage);
}
// Acknowledge the consumption of all messages at once
consumer.acknowledgeCumulative(msg);
consumer.close();
}
示例12: testFailedZeroQueueSizeBatchMessage
import org.apache.pulsar.client.api.ProducerConfiguration; //导入依赖的package包/类
@Test()
public void testFailedZeroQueueSizeBatchMessage() throws PulsarClientException {
int batchMessageDelayMs = 100;
ConsumerConfiguration conf = new ConsumerConfiguration();
conf.setSubscriptionType(SubscriptionType.Shared);
conf.setReceiverQueueSize(0);
Consumer consumer = pulsarClient.subscribe("persistent://prop-xyz/use/ns-abc/topic1", "my-subscriber-name",
conf);
ProducerConfiguration producerConf = new ProducerConfiguration();
if (batchMessageDelayMs != 0) {
producerConf.setBatchingEnabled(true);
producerConf.setBatchingMaxPublishDelay(batchMessageDelayMs, TimeUnit.MILLISECONDS);
producerConf.setBatchingMaxMessages(5);
}
Producer producer = pulsarClient.createProducer("persistent://prop-xyz/use/ns-abc/topic1", producerConf);
for (int i = 0; i < 10; i++) {
String message = "my-message-" + i;
producer.send(message.getBytes());
}
try {
consumer.receiveAsync().handle((ok, e) -> {
if (e == null) {
// as zero receiverQueueSize doesn't support batch message, must receive exception at callback.
Assert.fail();
}
return null;
});
} finally {
consumer.close();
}
}
示例13: testCleanProducer
import org.apache.pulsar.client.api.ProducerConfiguration; //导入依赖的package包/类
@Test
public void testCleanProducer() throws Exception {
log.info("-- Starting {} test --", methodName);
ConsumerConfiguration conf = new ConsumerConfiguration();
conf.setSubscriptionType(SubscriptionType.Exclusive);
ProducerConfiguration producerConf = new ProducerConfiguration();
admin.clusters().createCluster("global", new ClusterData());
admin.namespaces().createNamespace("my-property/global/lookup");
ClientConfiguration clientConf = new ClientConfiguration();
final int operationTimeOut = 500;
clientConf.setStatsInterval(0, TimeUnit.SECONDS);
clientConf.setOperationTimeout(operationTimeOut, TimeUnit.MILLISECONDS);
PulsarClient pulsarClient = PulsarClient.create(lookupUrl.toString(), clientConf);
CountDownLatch latch = new CountDownLatch(1);
pulsarClient.createProducerAsync("persistent://my-property/global/lookup/my-topic1", producerConf)
.handle((producer, e) -> {
latch.countDown();
return null;
});
latch.await(operationTimeOut+1000, TimeUnit.MILLISECONDS);
Field prodField = PulsarClientImpl.class.getDeclaredField("producers");
prodField.setAccessible(true);
@SuppressWarnings("unchecked")
IdentityHashMap<ProducerBase, Boolean> producers = (IdentityHashMap<ProducerBase, Boolean>) prodField
.get(pulsarClient);
assertTrue(producers.isEmpty());
pulsarClient.close();
log.info("-- Exiting {} test --", methodName);
}
示例14: 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();
}
示例15: 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();
}