本文整理汇总了Java中org.apache.pulsar.client.api.Producer类的典型用法代码示例。如果您正苦于以下问题:Java Producer类的具体用法?Java Producer怎么用?Java Producer使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Producer类属于org.apache.pulsar.client.api包,在下文中一共展示了Producer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: readEntries
import org.apache.pulsar.client.api.Producer; //导入依赖的package包/类
@Override
protected void readEntries(Producer producer) {
this.producer = (ProducerImpl) producer;
if (STATE_UPDATER.compareAndSet(this, State.Starting, State.Started)) {
log.info("[{}][{} -> {}] Created replicator producer", topicName, localCluster, remoteCluster);
backOff.reset();
} else {
log.info(
"[{}][{} -> {}] Replicator was stopped while creating the producer. Closing it. Replicator state: {}",
topicName, localCluster, remoteCluster, STATE_UPDATER.get(this));
STATE_UPDATER.set(this, State.Stopping);
closeProducerAsync();
return;
}
}
示例2: testPartitionedTopicNameWithSpecialCharacter
import org.apache.pulsar.client.api.Producer; //导入依赖的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: testDeletePartitionedTopic
import org.apache.pulsar.client.api.Producer; //导入依赖的package包/类
@Test(timeOut = 30000)
public void testDeletePartitionedTopic() throws Exception {
int numPartitions = 4;
DestinationName dn = DestinationName.get("persistent://my-property/use/my-ns/my-partitionedtopic6");
admin.persistentTopics().createPartitionedTopic(dn.toString(), numPartitions);
Producer producer = pulsarClient.createProducer(dn.toString());
Consumer consumer = pulsarClient.subscribe(dn.toString(), "my-sub");
consumer.unsubscribe();
consumer.close();
producer.close();
admin.persistentTopics().deletePartitionedTopic(dn.toString());
Producer producer1 = pulsarClient.createProducer(dn.toString());
if (producer1 instanceof PartitionedProducerImpl) {
Assert.fail("should fail since partitioned topic was deleted");
}
}
示例4: testMockBrokerService
import org.apache.pulsar.client.api.Producer; //导入依赖的package包/类
@Test
public void testMockBrokerService() throws Exception {
// test default actions of mock broker service
try {
PulsarClient client = PulsarClient.create("http://127.0.0.1:" + WEB_SERVICE_PORT);
ConsumerConfiguration conf = new ConsumerConfiguration();
conf.setSubscriptionType(SubscriptionType.Exclusive);
Consumer consumer = client.subscribe("persistent://prop/use/ns/t1", "sub1", conf);
Producer producer = client.createProducer("persistent://prop/use/ns/t1");
Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT);
producer.send("message".getBytes());
Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT);
consumer.unsubscribe();
producer.close();
consumer.close();
client.close();
} catch (Exception e) {
fail("None of the mocked operations should throw a client side exception");
}
}
示例5: publishMessagesBase
import org.apache.pulsar.client.api.Producer; //导入依赖的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;
}
示例6: testDeleteNamespace
import org.apache.pulsar.client.api.Producer; //导入依赖的package包/类
/**
* Verifies that deleteNamespace cleans up policies(global,local), bundle cache and bundle ownership
*
* @throws Exception
*/
@Test
public void testDeleteNamespace() throws Exception {
final String namespace = this.testProperty + "/use/deleteNs";
admin.namespaces().createNamespace(namespace, 100);
assertEquals(admin.namespaces().getPolicies(namespace).bundles.numBundles, 100);
// (1) Force topic creation and namespace being loaded
final String topicName = "persistent://" + namespace + "/my-topic";
DestinationName destination = DestinationName.get(topicName);
Producer producer = pulsarClient.createProducer(topicName);
producer.close();
NamespaceBundle bundle1 = pulsar.getNamespaceService().getBundle(destination);
// (2) Delete topic
admin.persistentTopics().delete(topicName);
// (3) Delete ns
admin.namespaces().deleteNamespace(namespace);
// (4) check bundle
NamespaceBundle bundle2 = pulsar.getNamespaceService().getBundle(destination);
assertNotEquals(bundle1.getBundleRange(), bundle2.getBundleRange());
// returns full bundle if policies not present
assertEquals("0x00000000_0xffffffff", bundle2.getBundleRange());
}
示例7: testNamespaceSplitBundle
import org.apache.pulsar.client.api.Producer; //导入依赖的package包/类
@Test
public void testNamespaceSplitBundle() throws Exception {
// Force to create a destination
final String namespace = "prop-xyz/use/ns1";
final String topicName = (new StringBuilder("persistent://")).append(namespace).append("/ds2").toString();
Producer producer = pulsarClient.createProducer(topicName);
producer.send("message".getBytes());
publishMessagesOnPersistentTopic(topicName, 0);
assertEquals(admin.persistentTopics().getList(namespace), Lists.newArrayList(topicName));
try {
admin.namespaces().splitNamespaceBundle(namespace, "0x00000000_0xffffffff", true);
} catch (Exception e) {
fail("split bundle shouldn't have thrown exception");
}
// bundle-factory cache must have updated split bundles
NamespaceBundles bundles = bundleFactory.getBundles(NamespaceName.get(namespace));
String[] splitRange = { namespace + "/0x00000000_0x7fffffff", namespace + "/0x7fffffff_0xffffffff" };
for (int i = 0; i < bundles.getBundles().size(); i++) {
assertEquals(bundles.getBundles().get(i).toString(), splitRange[i]);
}
producer.close();
}
示例8: testSimpleProducerEvents
import org.apache.pulsar.client.api.Producer; //导入依赖的package包/类
@Test
public void testSimpleProducerEvents() throws Exception {
final String topicName = "persistent://prop/use/ns-abc/topic0";
// 1. producer connect
Producer producer = pulsarClient.createProducer(topicName);
PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName);
assertNotNull(topicRef);
assertEquals(topicRef.getProducers().size(), 1);
// 2. producer publish messages
for (int i = 0; i < 10; i++) {
String message = "my-message-" + i;
producer.send(message.getBytes());
}
rolloverPerIntervalStats();
assertTrue(topicRef.getProducers().values().iterator().next().getStats().msgRateIn > 0.0);
// 3. producer disconnect
producer.close();
Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT);
assertEquals(topicRef.getProducers().size(), 0);
}
示例9: testSimpleTermination
import org.apache.pulsar.client.api.Producer; //导入依赖的package包/类
@Test
public void testSimpleTermination() throws Exception {
Producer producer = pulsarClient.createProducer(topicName);
/* MessageId msgId1 = */producer.send("test-msg-1".getBytes());
/* MessageId msgId2 = */producer.send("test-msg-2".getBytes());
MessageId msgId3 = producer.send("test-msg-3".getBytes());
MessageId lastMessageId = admin.persistentTopics().terminateTopicAsync(topicName).get();
assertEquals(lastMessageId, msgId3);
try {
producer.send("test-msg-4".getBytes());
fail("Should have thrown exception");
} catch (PulsarClientException.TopicTerminatedException e) {
// Expected
}
}
示例10: testCreateProducerOnTerminatedTopic
import org.apache.pulsar.client.api.Producer; //导入依赖的package包/类
@Test
public void testCreateProducerOnTerminatedTopic() throws Exception {
Producer producer = pulsarClient.createProducer(topicName);
/* MessageId msgId1 = */producer.send("test-msg-1".getBytes());
/* MessageId msgId2 = */producer.send("test-msg-2".getBytes());
MessageId msgId3 = producer.send("test-msg-3".getBytes());
MessageId lastMessageId = admin.persistentTopics().terminateTopicAsync(topicName).get();
assertEquals(lastMessageId, msgId3);
try {
pulsarClient.createProducer(topicName);
fail("Should have thrown exception");
} catch (PulsarClientException.TopicTerminatedException e) {
// Expected
}
}
示例11: testPartitions
import org.apache.pulsar.client.api.Producer; //导入依赖的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();
}
示例12: main
import org.apache.pulsar.client.api.Producer; //导入依赖的package包/类
public static void main(String[] args) throws PulsarClientException, InterruptedException, IOException {
PulsarClient pulsarClient = PulsarClient.create("http://127.0.0.1:8080");
Producer producer = pulsarClient.createProducer("persistent://my-property/use/my-ns/my-topic");
while (true) {
try {
producer.send("my-message".getBytes());
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
break;
}
}
pulsarClient.close();
}
示例13: start
import org.apache.pulsar.client.api.Producer; //导入依赖的package包/类
public void start() throws Exception {
Producer producer = getNewProducer();
final Consumer consumer = consumerFuture.get();
while (!stop.get()) {
final MutableBoolean wellnessFlag = new MutableBoolean();
final Function<Throwable, ? extends MessageId> exceptionHandler = e -> {
// Unset the well flag in the case of an exception so we can
// try to get a new Producer.
wellnessFlag.value = false;
return null;
};
while (!stop.get() && wellnessFlag.value) {
producer.sendAsync(payload.get()).exceptionally(exceptionHandler);
rateLimiter.acquire();
}
producer.closeAsync();
if (!stop.get()) {
// The Producer failed due to an exception: attempt to get
// another producer.
producer = getNewProducer();
} else {
// We are finished: close the consumer.
consumer.closeAsync();
}
}
}
示例14: main
import org.apache.pulsar.client.api.Producer; //导入依赖的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();
}
示例15: testSimpleConsumer
import org.apache.pulsar.client.api.Producer; //导入依赖的package包/类
@Test
public void testSimpleConsumer() throws Exception {
String topic = "persistent://sample/standalone/ns/testSimpleConsumer";
Properties props = new Properties();
props.put("bootstrap.servers", lookupUrl.toString());
props.put("group.id", "my-subscription-name");
props.put("enable.auto.commit", "false");
props.put("key.deserializer", StringDeserializer.class.getName());
props.put("value.deserializer", StringDeserializer.class.getName());
Consumer<String, String> consumer = new PulsarKafkaConsumer<>(props);
consumer.subscribe(Arrays.asList(topic));
Producer pulsarProducer = pulsarClient.createProducer(topic);
for (int i = 0; i < 10; i++) {
Message msg = MessageBuilder.create().setKey(Integer.toString(i)).setContent(("hello-" + i).getBytes())
.build();
pulsarProducer.send(msg);
}
for (int i = 0; i < 10; i++) {
ConsumerRecords<String, String> records = consumer.poll(100);
assertEquals(records.count(), 1);
int idx = i;
records.forEach(record -> {
assertEquals(record.key(), Integer.toString(idx));
assertEquals(record.value(), "hello-" + idx);
});
consumer.commitSync();
}
consumer.close();
}