本文整理汇总了C#中ConnectionFactory.CreateConnection方法的典型用法代码示例。如果您正苦于以下问题:C# ConnectionFactory.CreateConnection方法的具体用法?C# ConnectionFactory.CreateConnection怎么用?C# ConnectionFactory.CreateConnection使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ConnectionFactory
的用法示例。
在下文中一共展示了ConnectionFactory.CreateConnection方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestProducerSendWithTimeout
public void TestProducerSendWithTimeout()
{
int timeout = 1500;
Uri uri = new Uri(string.Format("mock://localhost:61616?connection.RequestTimeout={0}&transport.respondToMessages=false", timeout));
ConnectionFactory factory = new ConnectionFactory(uri);
using(IConnection connection = factory.CreateConnection())
using(ISession session = connection.CreateSession())
{
IDestination destination = session.GetTopic("Test");
using(IMessageProducer producer = session.CreateProducer(destination))
{
ITextMessage message = session.CreateTextMessage("Hello World");
for(int i = 0; i < 10; ++i)
{
DateTime start = DateTime.Now;
try
{
producer.Send(message);
Assert.Fail("Expected a RequestTimedOutException");
}
catch(RequestTimedOutException)
{
}
TimeSpan elapsed = DateTime.Now - start;
// Make sure we timed out.
Assert.GreaterOrEqual((int) elapsed.TotalMilliseconds, timeout - 10, "Did not reach timeout limit.");
}
}
}
}
示例2: CreateConnection
private IConnection CreateConnection()
{
IConnectionFactory factory = new ConnectionFactory(jmsServer);
IConnection connection = factory.CreateConnection(userId, password);
connection.Start();
return connection;
}
示例3: TestHundredsOfConnectionsWithRandomHeartbeatInterval
public void TestHundredsOfConnectionsWithRandomHeartbeatInterval()
{
if (!LongRunningTestsEnabled())
{
Console.WriteLine("RABBITMQ_LONG_RUNNING_TESTS is not set, skipping test");
return;
}
var rnd = new Random();
List<IConnection> xs = new List<IConnection>();
for (var i = 0; i < 200; i++)
{
var n = Convert.ToUInt16(rnd.Next(2, 6));
var cf = new ConnectionFactory() { RequestedHeartbeat = n, AutomaticRecoveryEnabled = false };
var conn = cf.CreateConnection();
xs.Add(conn);
var ch = conn.CreateModel();
bool wasShutdown = false;
conn.ConnectionShutdown += (sender, evt) =>
{
CheckInitiator(evt);
};
}
SleepFor(60);
foreach (var x in xs)
{
x.Close();
}
}
示例4: TestHundredsOfConnectionsWithRandomHeartbeatInterval
public void TestHundredsOfConnectionsWithRandomHeartbeatInterval()
{
var rnd = new Random();
List<IConnection> xs = new List<IConnection>();
for (var i = 0; i < 200; i++)
{
var n = Convert.ToUInt16(rnd.Next(2, 6));
var cf = new ConnectionFactory() { RequestedHeartbeat = n, AutomaticRecoveryEnabled = false };
var conn = cf.CreateConnection();
xs.Add(conn);
var ch = conn.CreateModel();
bool wasShutdown = false;
conn.ConnectionShutdown += (sender, evt) =>
{
CheckInitiator(evt);
};
}
SleepFor(60);
foreach (var x in xs)
{
x.Close();
}
}
示例5: TestNoCopyOnSend
public void TestNoCopyOnSend()
{
Uri uri = new Uri("mock://localhost:61616?connection.CopyMessageOnSend=false");
ConnectionFactory factory = new ConnectionFactory(uri);
using(IConnection connection = factory.CreateConnection())
using(ISession session = connection.CreateSession())
{
IDestination destination = session.GetTopic("Test");
using(IMessageProducer producer = session.CreateProducer(destination))
{
ITextMessage message = session.CreateTextMessage();
for(int i = 0; i < 10; ++i)
{
try
{
message.Properties["TribbleName"] = "Tribble" + i.ToString();
message.Text = "The Trouble with Tribbles - " + i.ToString();
producer.Send(message);
}
catch(MessageNotWriteableException)
{
Assert.Greater(i, 0);
Assert.Less(i, 10);
}
}
}
}
}
示例6: TestRedelivered
public void TestRedelivered()
{
// enqueue several messages
PurgeDatabase();
PurgeAndFillQueue();
// receive just one
INetTxConnectionFactory factory = new NetTxConnectionFactory(ReplaceEnvVar(connectionURI));
using (INetTxConnection connection = factory.CreateNetTxConnection())
{
connection.Start();
using (INetTxSession session = connection.CreateNetTxSession())
{
IQueue queue = session.GetQueue(testQueueName);
// read message from queue and insert into db table
using (IMessageConsumer consumer = session.CreateConsumer(queue))
{
using (TransactionScope scoped = new TransactionScope(TransactionScopeOption.RequiresNew))
using (SqlConnection sqlConnection = new SqlConnection(sqlConnectionString))
using (SqlCommand sqlInsertCommand = new SqlCommand())
{
sqlConnection.Open();
sqlInsertCommand.Connection = sqlConnection;
ITextMessage message = consumer.Receive(TimeSpan.FromMilliseconds(10000)) as ITextMessage;
sqlInsertCommand.CommandText =
string.Format("INSERT INTO {0} VALUES ({1})", testTable, Convert.ToInt32(message.Text));
sqlInsertCommand.ExecuteNonQuery();
scoped.Complete();
}
}
session.Close();
}
}
// check that others message have status redelivered = false
IConnectionFactory checkFactory = new ConnectionFactory(ReplaceEnvVar(connectionURI));
using (IConnection connection = checkFactory.CreateConnection())
{
connection.Start();
using (ISession session = connection.CreateSession())
using (IQueueBrowser browser = session.CreateBrowser(session.GetQueue(testQueueName)))
{
IEnumerator enumerator = browser.GetEnumerator();
while (enumerator.MoveNext())
{
IMessage msg = enumerator.Current as IMessage;
Assert.IsNotNull(msg, "message is not in the queue!");
Assert.IsFalse(msg.NMSRedelivered, "message is redelivered!");
}
}
}
}
示例7: Init
public override void Init()
{
var connFactory = new ConnectionFactory()
{
AutomaticRecoveryEnabled = false
};
Conn = connFactory.CreateConnection();
Model = Conn.CreateModel();
}
示例8: RabbitMqService
public RabbitMqService(string host)
{
_host = host;
var factory = new ConnectionFactory
{
Protocol = Protocols.FromEnvironment(),
HostName = _host
};
_connection = factory.CreateConnection();
}
示例9: OpenConnection
static IModel OpenConnection(string machine, string userName, string password)
{
ConnectionFactory factory = new ConnectionFactory
{
HostName = machine,
Port = AmqpTcpEndpoint.UseDefaultPort,
UserName = userName,
Password = password,
};
IConnection conn = factory.CreateConnection();
return conn.CreateModel();
}
示例10: DeleteQueue
public static void DeleteQueue(string uri, string queueName)
{
ConnectionFactory factory = new ConnectionFactory
{
Uri = uri,
};
using (IConnection conn = factory.CreateConnection())
using (IModel channel = conn.CreateModel())
{
channel.QueueUnbind(queueName, queueName, string.Empty, null);
channel.ExchangeDelete(queueName);
channel.QueueDelete(queueName);
}
}
示例11: TestHandleLeak
public void TestHandleLeak()
{
var cf = new ConnectionFactory();
var me = Process.GetCurrentProcess();
var n = me.HandleCount;
Console.WriteLine("{0} handles before the test...", me.HandleCount);
for (var i = 0; i < 1000; i++)
{
using (var conn = cf.CreateConnection())
{
}
}
Thread.Sleep(TimeSpan.FromSeconds(10));
Console.WriteLine("{0} handles after the test...", me.HandleCount);
Assert.That(me.HandleCount, Is.LessThanOrEqualTo(n));
}
示例12: PerformLeakTest
protected void PerformLeakTest(ConnectionFactory cf)
{
var me = Process.GetCurrentProcess();
var n = me.HandleCount;
Console.WriteLine("{0} handles before the test...", me.HandleCount);
for (var i = 0; i < 1000; i++)
{
var conn = cf.CreateConnection();
conn.Close();
}
GC.Collect();
GC.WaitForPendingFinalizers();
Thread.Sleep(TimeSpan.FromSeconds(10));
me = Process.GetCurrentProcess();
Console.WriteLine("{0} handles after the test...", me.HandleCount);
// allow for a 20% margin of error, as GC behaviour and native handle
// release is difficult to predict
Assert.That(me.HandleCount, Is.LessThanOrEqualTo(n + 200));
}
示例13: TestCloseWithFaultyConsumer
public void TestCloseWithFaultyConsumer()
{
ConnectionFactory connFactory = new ConnectionFactory();
IConnection c = connFactory.CreateConnection();
IModel m = Conn.CreateModel();
object o = new object();
string q = GenerateQueueName();
m.QueueDeclare(q, false, false, false, null);
CallbackExceptionEventArgs ea = null;
m.CallbackException += (_, evt) => {
ea = evt;
c.Close();
Monitor.PulseAll(o);
};
m.BasicConsume(q, true, new FaultyConsumer(Model));
m.BasicPublish("", q, null, encoding.GetBytes("message"));
WaitOn(o);
Assert.IsNotNull(ea);
Assert.AreEqual(c.IsOpen, false);
Assert.AreEqual(c.CloseReason.ReplyCode, 200);
}
示例14: CreateQueue
public static void CreateQueue(string uri, string queueName, bool durableMessages, bool createExchange)
{
ConnectionFactory factory = new ConnectionFactory
{
Uri = uri,
};
using (IConnection conn = factory.CreateConnection())
{
IModel channel = conn.CreateModel();
channel.QueueDeclare(
queue: queueName,
durable: durableMessages,
exclusive: false,
autoDelete: false,
arguments: null);
if (createExchange)
{
CreateExchange(channel, queueName, durableMessages);
}
}
}
示例15: Init
public virtual void Init()
{
var connFactory = new ConnectionFactory();
Conn = connFactory.CreateConnection();
Model = Conn.CreateModel();
}