本文整理汇总了Java中io.nats.client.ConnectionFactory.createConnection方法的典型用法代码示例。如果您正苦于以下问题:Java ConnectionFactory.createConnection方法的具体用法?Java ConnectionFactory.createConnection怎么用?Java ConnectionFactory.createConnection使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.nats.client.ConnectionFactory
的用法示例。
在下文中一共展示了ConnectionFactory.createConnection方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: start
import io.nats.client.ConnectionFactory; //导入方法依赖的package包/类
@Override
public void start(Map<String, String> map) {
LOG.info("Start the Nats Sink Task");
String[] nhost = map.get(NATS_URL).split(",");
this.subject = map.get(NATS_SUBJECT);
try {
if (nhost.length == 1)
this.nc = Nats.connect(nhost[0]);
else{
ConnectionFactory cf = new ConnectionFactory();
cf.setServers(nhost);
cf.setMaxReconnect(5);
cf.setReconnectWait(2000);
cf.setNoRandomize(true);
this.nc = cf.createConnection();
}
LOG.info("Connected to the next NATS URL(master) : " + this.nc.getConnectedUrl());
} catch (IOException e){
LOG.error(e.getMessage(), e);
}
}
示例2: start
import io.nats.client.ConnectionFactory; //导入方法依赖的package包/类
@Override
public void start(Map<String, String> map) {
LOG.info("Start the Nats Source Task");
String nsubject = map.get(NATS_SUBJECT);
String[] nhost = map.get(NATS_URL).split(",");
String nQueueGroup = map.get(NATS_QUEUE_GROUP);
this.ktopic = map.get(KAFKA_TOPIC);
try {
if (nhost.length == 1)
this.nc = Nats.connect(nhost[0]);
else if (nhost.length >= 2){
ConnectionFactory cf = new ConnectionFactory();
cf.setServers(nhost);
cf.setMaxReconnect(5);
cf.setReconnectWait(2000);
cf.setNoRandomize(true);
this.nc = cf.createConnection();
}
else
throw new ConnectException("No NATS URL");
LOG.info("Connected to the next NATS URL(master) : " + this.nc.getConnectedUrl());
} catch (IOException e){
throw new ConnectException(e.getMessage(), e);
}
this.nc.subscribe(nsubject, nQueueGroup, message -> {
LOG.debug("Sending the next message : {}", message);
Schema recordSchema = NatsSourceConverter.getRecordSchema();
Struct recordStruct = NatsSourceConverter.getRecordStruct(recordSchema, message.getReplyTo(),
new String(message.getData()));
SourceRecord sc = new SourceRecord(null,null,
ktopic ,Schema.STRING_SCHEMA, message.getSubject(),
recordSchema, recordStruct);
mQueue.add(sc);
});
}
示例3: receive
import io.nats.client.ConnectionFactory; //导入方法依赖的package包/类
/** Create a socket connection and receive data until receiver is stopped
* @throws IncompleteException
* @throws TimeoutException
* @throws IOException
* @throws Exception **/
protected void receive() throws IncompleteException, IOException, TimeoutException {
// Make connection and initialize streams
final ConnectionFactory connectionFactory = new ConnectionFactory(getEnrichedProperties());
final Connection connection = connectionFactory.createConnection();
logger.info("A NATS from '{}' to Spark Connection has been created for '{}', sharing Queue '{}'.", connection.getConnectedUrl(), this, queue);
for (String subject: getSubjects()) {
final Subscription sub = connection.subscribe(subject, queue, getMessageHandler());
logger.info("Listening on {}.", subject);
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable(){
@Override
public void run() {
logger.debug("Caught CTRL-C, shutting down gracefully..." + this);
try {
sub.unsubscribe();
} catch (IOException e) {
if (logger.isDebugEnabled()) {
logger.error("Exception while unsubscribing " + e.toString());
}
}
connection.close();
}
}));
}
}
开发者ID:Logimethods,项目名称:nats-connector-spark,代码行数:33,代码来源:OmnipotentStandardNatsToSparkConnector.java
示例4: run
import io.nats.client.ConnectionFactory; //导入方法依赖的package包/类
@Override
public void run() {
try {
logger.debug("NATS Publisher ({}): Starting", id);
ConnectionFactory cf = new ConnectionFactory(natsUrl);
io.nats.client.Connection c = cf.createConnection();
logger.debug("A NATS Connection to '{}' has been created.", c.getConnectedUrl());
setReady();
for (int i = 0; i < testCount; i++) {
final String payload = NATS_PAYLOAD + INCR.getAndIncrement();
c.publish(subject, payload.getBytes());
logger.trace("Publish '{}' to '{}'.", payload, subject);
tallyMessage();
}
c.flush();
logger.debug("NATS Publisher ({}): Published {} messages.", id, testCount);
setComplete();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
示例5: run
import io.nats.client.ConnectionFactory; //导入方法依赖的package包/类
@Override
public void run() {
try {
logger.debug("NATS Publisher ({}): Starting", id);
ConnectionFactory cf = new ConnectionFactory(natsUrl);
io.nats.client.Connection c = cf.createConnection();
logger.debug("A NATS Connection to '{}' has been created.", c.getConnectedUrl());
setReady();
for (int i = 0; i < testCount; i++) {
final ByteBuffer buffer = ByteBuffer.allocate(4);
final int payload = NATS_PAYLOAD_INT + INCR.getAndIncrement();
buffer.putInt(payload);
c.publish(subject, buffer.array());
logger.trace("Publish '{}' to '{}'.", payload, subject);
tallyMessage();
}
c.flush();
logger.debug("NATS Publisher ({}): Published {} messages.", id, testCount);
setComplete();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
示例6: testLoadConsumer
import io.nats.client.ConnectionFactory; //导入方法依赖的package包/类
@Test
public void testLoadConsumer() throws InterruptedException, IOException, TimeoutException {
mockResultEndpoint.setExpectedMessageCount(10000);
ConnectionFactory cf = new ConnectionFactory("nats://localhost:4222");
Connection connection = cf.createConnection();
for (int i = 0; i < 10000; i++) {
connection.publish("test", ("test" + i).getBytes());
}
mockResultEndpoint.assertIsSatisfied();
}
示例7: getConnection
import io.nats.client.ConnectionFactory; //导入方法依赖的package包/类
private Connection getConnection() throws TimeoutException, IOException {
Properties prop = getEndpoint().getNatsConfiguration().createProperties();
ConnectionFactory factory = new ConnectionFactory(prop);
connection = factory.createConnection();
return connection;
}
示例8: getConnection
import io.nats.client.ConnectionFactory; //导入方法依赖的package包/类
private Connection getConnection() throws IOException, InterruptedException, TimeoutException {
Properties prop = getEndpoint().getNatsConfiguration().createProperties();
ConnectionFactory factory = new ConnectionFactory(prop);
connection = factory.createConnection();
return connection;
}