本文整理汇总了C#中NATS.Client.ConnectionFactory类的典型用法代码示例。如果您正苦于以下问题:C# ConnectionFactory类的具体用法?C# ConnectionFactory怎么用?C# ConnectionFactory使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ConnectionFactory类属于NATS.Client命名空间,在下文中一共展示了ConnectionFactory类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestClientAutoUnsub
public void TestClientAutoUnsub()
{
using (IConnection c = new ConnectionFactory().CreateConnection())
{
long received = 0;
int max = 10;
using (ISyncSubscription s = c.SubscribeSync("foo"))
{
s.AutoUnsubscribe(max);
for (int i = 0; i < max * 2; i++)
{
c.Publish("foo", null);
}
c.Flush();
Thread.Sleep(100);
try
{
while (true)
{
s.NextMessage(0);
received++;
}
}
catch (NATSBadSubscriptionException) { /* ignore */ }
Assert.IsTrue(received == max);
Assert.IsFalse(s.IsValid);
}
}
}
示例2: TestReconnectDisallowedFlags
public void TestReconnectDisallowedFlags()
{
Options opts = ConnectionFactory.GetDefaultOptions();
opts.Url = "nats://localhost:22222";
opts.AllowReconnect = false;
Object testLock = new Object();
opts.ClosedEventHandler = (sender, args) =>
{
lock(testLock)
{
Monitor.Pulse(testLock);
}
};
using (NATSServer ns = utils.CreateServerOnPort(22222))
{
using (IConnection c = new ConnectionFactory().CreateConnection(opts))
{
lock (testLock)
{
ns.Shutdown();
Assert.IsTrue(Monitor.Wait(testLock, 1000));
}
}
}
}
示例3: TestCloseDisconnectedHandler
public void TestCloseDisconnectedHandler()
{
bool disconnected = false;
Object mu = new Object();
Options o = ConnectionFactory.GetDefaultOptions();
o.AllowReconnect = false;
o.DisconnectedEventHandler += (sender, args) => {
lock (mu)
{
disconnected = true;
Monitor.Pulse(mu);
}
};
IConnection c = new ConnectionFactory().CreateConnection(o);
lock (mu)
{
c.Close();
Monitor.Wait(mu, 20000);
}
Assert.IsTrue(disconnected);
// now test using.
disconnected = false;
lock (mu)
{
using (c = new ConnectionFactory().CreateConnection(o)) { };
Monitor.Wait(mu);
}
Assert.IsTrue(disconnected);
}
示例4: Run
public void Run(string[] args)
{
Stopwatch sw = null;
parseArgs(args);
banner();
Options opts = ConnectionFactory.GetDefaultOptions();
opts.Url = url;
using (IConnection c = new ConnectionFactory().CreateConnection(opts))
{
sw = Stopwatch.StartNew();
for (int i = 0; i < count; i++)
{
c.Request(subject, payload);
}
c.Flush();
sw.Stop();
System.Console.Write("Completed {0} requests in {1} seconds ", count, sw.Elapsed.TotalSeconds);
System.Console.WriteLine("({0} requests/second).",
(int)(count / sw.Elapsed.TotalSeconds));
printStats(c);
}
}
示例5: TestConnectionStatus
public void TestConnectionStatus()
{
IConnection c = new ConnectionFactory().CreateConnection();
Assert.AreEqual(ConnState.CONNECTED, c.State);
c.Close();
Assert.AreEqual(ConnState.CLOSED, c.State);
}
示例6: TestAsyncSubHandlerAPI
public void TestAsyncSubHandlerAPI()
{
using (IConnection c = new ConnectionFactory().CreateConnection())
{
int received = 0;
EventHandler<MsgHandlerEventArgs> h = (sender, args) =>
{
Interlocked.Increment(ref received);
};
using (IAsyncSubscription s = c.SubscribeAsync("foo", h))
{
c.Publish("foo", null);
c.Flush();
Thread.Sleep(500);
}
using (IAsyncSubscription s = c.SubscribeAsync("foo", "bar", h))
{
c.Publish("foo", null);
c.Flush();
Thread.Sleep(500);
}
if (received != 2)
{
Assert.Fail("Received ({0}) != 2", received);
}
}
}
示例7: TestBasicReconnectFunctionality
public void TestBasicReconnectFunctionality()
{
Options opts = utils.DefaultTestOptions;
opts.Url = "nats://localhost:22222";
opts.MaxReconnect = 2;
opts.ReconnectWait = 1000;
Object testLock = new Object();
Object msgLock = new Object();
opts.DisconnectedEventHandler = (sender, args) =>
{
lock (testLock)
{
Monitor.Pulse(testLock);
}
};
opts.ReconnectedEventHandler = (sender, args) =>
{
// NOOP
};
NATSServer ns = utils.CreateServerOnPort(22222);
using (IConnection c = new ConnectionFactory().CreateConnection(opts))
{
IAsyncSubscription s = c.SubscribeAsync("foo");
s.MessageHandler += (sender, args) =>
{
lock (msgLock)
{
Monitor.Pulse(msgLock);
}
};
s.Start();
c.Flush();
lock (testLock)
{
ns.Shutdown();
Assert.True(Monitor.Wait(testLock, 100000));
}
c.Publish("foo", Encoding.UTF8.GetBytes("Hello"));
// restart the server.
using (ns = utils.CreateServerOnPort(22222))
{
lock (msgLock)
{
c.Flush(50000);
Assert.True(Monitor.Wait(msgLock, 10000));
}
Assert.True(c.Stats.Reconnects == 1);
}
}
}
示例8: TestServersOption
public void TestServersOption()
{
IConnection c = null;
ConnectionFactory cf = new ConnectionFactory();
Options o = ConnectionFactory.GetDefaultOptions();
o.NoRandomize = true;
UnitTestUtilities.testExpectedException(
() => { cf.CreateConnection(); },
typeof(NATSNoServersException));
o.Servers = testServers;
UnitTestUtilities.testExpectedException(
() => { cf.CreateConnection(o); },
typeof(NATSNoServersException));
// Make sure we can connect to first server if running
using (NATSServer ns = utils.CreateServerOnPort(1222))
{
c = cf.CreateConnection(o);
Assert.IsTrue(testServers[0].Equals(c.ConnectedUrl));
c.Close();
}
// make sure we can connect to a non-first server.
using (NATSServer ns = utils.CreateServerOnPort(1227))
{
c = cf.CreateConnection(o);
Assert.IsTrue(testServers[5].Equals(c.ConnectedUrl));
c.Close();
}
}
示例9: Run
public void Run(string[] args)
{
parseArgs(args);
banner();
Options opts = ConnectionFactory.GetDefaultOptions();
opts.Url = url;
replyMsg.Data = Encoding.UTF8.GetBytes("reply");
using (IConnection c = new ConnectionFactory().CreateConnection(opts))
{
TimeSpan elapsed;
if (sync)
{
elapsed = receiveSyncSubscriber(c);
}
else
{
elapsed = receiveAsyncSubscriber(c);
}
System.Console.Write("Replied to {0} msgs in {1} seconds ", received, elapsed.TotalSeconds);
System.Console.WriteLine("({0} replies/second).",
(int)(received / elapsed.TotalSeconds));
printStats(c);
}
}
示例10: connectAndFail
private void connectAndFail(String url)
{
try
{
System.Console.WriteLine("Trying: " + url);
hitDisconnect = 0;
Options opts = ConnectionFactory.GetDefaultOptions();
opts.Url = url;
opts.DisconnectedEventHandler += handleDisconnect;
IConnection c = new ConnectionFactory().CreateConnection(url);
Assert.Fail("Expected a failure; did not receive one");
c.Close();
}
catch (Exception e)
{
if (e.Message.Contains("Authorization"))
{
System.Console.WriteLine("Success with expected failure: " + e.Message);
}
else
{
Assert.Fail("Unexpected exception thrown: " + e);
}
}
finally
{
if (hitDisconnect > 0)
Assert.Fail("The disconnect event handler was incorrectly invoked.");
}
}
示例11: TestAuthServers
public void TestAuthServers()
{
string[] plainServers = new string[] {
"nats://localhost:1222",
"nats://localhost:1224"
};
Options opts = utils.DefaultTestOptions;
opts.NoRandomize = true;
opts.Servers = plainServers;
opts.Timeout = 5000;
using (NATSServer as1 = utils.CreateServerWithConfig("auth_1222.conf"),
as2 = utils.CreateServerWithConfig("auth_1224.conf"))
{
Assert.ThrowsAny<NATSException>(() => new ConnectionFactory().CreateConnection(opts));
// Test that we can connect to a subsequent correct server.
string[] authServers = new string[] {
"nats://localhost:1222",
"nats://username:[email protected]:1224"};
opts.Servers = authServers;
using (IConnection c = new ConnectionFactory().CreateConnection(opts))
{
Assert.Equal(authServers[1], c.ConnectedUrl);
}
}
}
示例12: TestSimplePublish
public void TestSimplePublish()
{
using (IConnection c = new ConnectionFactory().CreateConnection())
{
c.Publish("foo", Encoding.UTF8.GetBytes("Hello World!"));
}
}
示例13: TestServerAutoUnsub
public void TestServerAutoUnsub()
{
using (IConnection c = new ConnectionFactory().CreateConnection())
{
long received = 0;
int max = 10;
using (IAsyncSubscription s = c.SubscribeAsync("foo"))
{
s.MessageHandler += (sender, arg) =>
{
System.Console.WriteLine("Received msg.");
received++;
};
s.AutoUnsubscribe(max);
s.Start();
for (int i = 0; i < (max * 2); i++)
{
c.Publish("foo", Encoding.UTF8.GetBytes("hello"));
}
c.Flush();
Thread.Sleep(500);
if (received != max)
{
Assert.Fail("Recieved ({0}) != max ({1})",
received, max);
}
Assert.IsFalse(s.IsValid);
}
}
}
示例14: Run
public void Run(string[] args)
{
parseArgs(args);
banner();
Options opts = ConnectionFactory.GetDefaultOptions();
opts.Url = url;
using (IConnection c = new ConnectionFactory().CreateConnection(opts))
{
TimeSpan elapsed;
if (sync)
{
elapsed = receiveSyncSubscriber(c);
}
else
{
elapsed = receiveAsyncSubscriber(c);
}
System.Console.Write("Received {0} msgs in {1} seconds ", count, elapsed.TotalSeconds);
System.Console.WriteLine("({0} msgs/second).",
(int)(count / elapsed.TotalSeconds));
printStats(c);
}
}
示例15: TestCustomObjectSerialization
public void TestCustomObjectSerialization()
{
using (IEncodedConnection c = new ConnectionFactory().CreateEncodedConnection())
{
Object mu = new Object();
SerializationTestObj origObj = new SerializationTestObj();
EventHandler<EncodedMessageEventArgs> eh = (sender, args) =>
{
// Ensure we blow up in the cast
SerializationTestObj so = (SerializationTestObj)args.ReceivedObject;
Assert.IsTrue(so.Equals(origObj));
lock (mu)
{
Monitor.Pulse(mu);
}
};
using (IAsyncSubscription s = c.SubscribeAsync("foo", eh))
{
lock (mu)
{
c.Publish("foo", new SerializationTestObj());
c.Flush();
Monitor.Wait(mu, 1000);
}
}
}
}