本文整理汇总了C#中ConnectionFactory类的典型用法代码示例。如果您正苦于以下问题:C# ConnectionFactory类的具体用法?C# ConnectionFactory怎么用?C# ConnectionFactory使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ConnectionFactory类属于命名空间,在下文中一共展示了ConnectionFactory类的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: UnitOfWork
public UnitOfWork(string nameOrConnectionString)
{
var connectionFactory = new ConnectionFactory(nameOrConnectionString);
_connection = connectionFactory.Create();
_connection.Open();
_transaction = _connection.BeginTransaction();
}
示例3: Main
public static void Main()
{
var factory = new ConnectionFactory() { HostName = "localhost" };
using( var connection = factory.CreateConnection() )
using( var channel = connection.CreateModel() )
{
channel.QueueDeclare( queue: "task_queue", durable: true, exclusive: false, autoDelete: false, arguments: null );
channel.BasicQos( prefetchSize: 0, prefetchCount: 1, global: false );
var consumer = new QueueingBasicConsumer( channel );
channel.BasicConsume( queue: "task_queue", noAck: false, consumer: consumer );
Console.WriteLine( " [*] Waiting for messages. To exit press CTRL+C" );
while( true )
{
var ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue();
var body = ea.Body;
var message = Encoding.UTF8.GetString( body );
Console.WriteLine( " [x] Received {0}", message );
int dots = message.Split( '.' ).Length - 1;
Thread.Sleep( dots * 1000 );
Console.WriteLine( " [x] Done" );
channel.BasicAck( deliveryTag: ea.DeliveryTag, multiple: false );
}
}
}
示例4: Main
public static void Main()
{
var factory = new ConnectionFactory() { HostName = "localhost" };
using(var connection = factory.CreateConnection())
using(var channel = connection.CreateModel())
{
channel.ExchangeDeclare(exchange: "logs", type: "fanout");
var queueName = channel.QueueDeclare().QueueName;
channel.QueueBind(queue: queueName, exchange: "logs", routingKey: "");
Console.WriteLine(" [*] Waiting for logs.");
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var body = ea.Body;
var message = Encoding.UTF8.GetString(body);
Console.WriteLine(" [x] {0}", message);
};
channel.BasicConsume(queue: queueName, noAck: true, consumer: consumer);
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
}
}
示例5: GetProviderFromConfigName
public static IGroupDataProvider GetProviderFromConfigName(ILog log, IConfig groupsConfig, string configName)
{
switch (configName)
{
case "XmlRpc":
string ServiceURL = groupsConfig.GetString("XmlRpcServiceURL");
bool DisableKeepAlive = groupsConfig.GetBoolean("XmlRpcDisableKeepAlive", false);
string ServiceReadKey = groupsConfig.GetString("XmlRpcServiceReadKey", String.Empty);
string ServiceWriteKey = groupsConfig.GetString("XmlRpcServiceWriteKey", String.Empty);
log.InfoFormat("[GROUPS]: XmlRpc Service URL set to: {0}", ServiceURL);
return new XmlRpcGroupDataProvider(ServiceURL, DisableKeepAlive, ServiceReadKey, ServiceWriteKey);
case "Native":
string dbType = groupsConfig.GetString("NativeProviderDBType");
string connStr = groupsConfig.GetString("NativeProviderConnString");
ConnectionFactory connFactory = new ConnectionFactory(dbType, connStr);
return new NativeGroupDataProvider(connFactory);
}
return null;
}
示例6: 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();
}
}
示例7: Main
public static void Main()
{
ConnectionFactory factory = new ConnectionFactory();
factory.HostName = "localhost";
using (IConnection connection = factory.CreateConnection())
using (IModel channel = connection.CreateModel()) {
channel.ExchangeDeclare("logs", "fanout");
string queue_name = channel.QueueDeclare();
channel.QueueBind(queue_name, "logs", "");
QueueingBasicConsumer consumer = new QueueingBasicConsumer(channel);
channel.BasicConsume(queue_name, true, consumer);
Console.WriteLine(" [*] Waiting for logs." +
"To exit press CTRL+C");
while(true) {
BasicDeliverEventArgs ea =
(BasicDeliverEventArgs)consumer.Queue.Dequeue();
byte[] body = ea.Body;
string message = System.Text.Encoding.UTF8.GetString(body);
Console.WriteLine(" [x] {0}", message);
}
}
}
示例8: TestThatHeartbeatWriterWithTLSEnabled
public void TestThatHeartbeatWriterWithTLSEnabled()
{
if (!LongRunningTestsEnabled())
{
Console.WriteLine("RABBITMQ_LONG_RUNNING_TESTS is not set, skipping test");
return;
}
var cf = new ConnectionFactory()
{
RequestedHeartbeat = heartbeatTimeout,
AutomaticRecoveryEnabled = false
};
string sslDir = IntegrationFixture.CertificatesDirectory();
if (null == sslDir)
{
Console.WriteLine("SSL_CERT_DIR is not configured, skipping test");
return;
}
cf.Ssl.ServerName = System.Net.Dns.GetHostName();
Assert.IsNotNull(sslDir);
cf.Ssl.CertPath = sslDir + "/client/keycert.p12";
string p12Password = Environment.GetEnvironmentVariable("PASSWORD");
Assert.IsNotNull(p12Password, "missing PASSWORD env var");
cf.Ssl.CertPassphrase = p12Password;
cf.Ssl.Enabled = true;
RunSingleConnectionTest(cf);
}
示例9: 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);
}
}
}
}
}
示例10: 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!");
}
}
}
}
示例11: Main
public static void Main()
{
var factory = new ConnectionFactory() { HostName = "localhost" };
using (var connection = factory.CreateConnection())
{
using (var channel = connection.CreateModel())
{
channel.ExchangeDeclare("logs", "fanout");
var queueName = channel.QueueDeclare();
channel.QueueBind(queueName, "logs", "");
var consumer = new QueueingBasicConsumer(channel);
channel.BasicConsume(queueName, true, consumer);
Console.WriteLine(" [*] Waiting for logs." +
"To exit press CTRL+C");
while (true)
{
var ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue();
var body = ea.Body;
var message = Encoding.UTF8.GetString(body);
Console.WriteLine(" [x] {0}", message);
}
}
}
}
示例12: Main
public static void Main()
{
ConnectionFactory factory = new ConnectionFactory();
factory.HostName = "127.0.0.1";
using (IConnection connection = factory.CreateConnection())
using (IModel channel = connection.CreateModel())
{
channel.QueueDeclare(QueueName, false, false, false, null);
QueueingBasicConsumer consumer = new QueueingBasicConsumer(channel);
bool autoAck = true;
channel.BasicConsume(QueueName, autoAck, consumer);
System.Console.WriteLine(" [*] Message List\n" +
"To exit press CTRL+C");
while (true)
{
BasicDeliverEventArgs ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue();
byte[] body = ea.Body;
string message = System.Text.Encoding.UTF8.GetString(body);
System.Console.WriteLine(" [x] Received {0}", message);
}
}
}
示例13: NewTask
public void NewTask()
{
string filepath = Utils.GetFullPathFileName("Chegou.png");
byte[] body = Utils.GetFileAsBytesOrNull (filepath);
var factory = new ConnectionFactory() { HostName = "diablo" };
using(var connection = factory.CreateConnection())
using(var channel = connection.CreateModel())
{
//channel.QueueDeclare("Work_Queue"); // version shup
channel.QueueDeclare("Work_Queue",true,false,false,null);
var properties = channel.CreateBasicProperties();
properties.SetPersistent(true);
channel.BasicPublish(exchange: "",
routingKey: "Work_Queue",
basicProperties: properties,
body: body);
Text text ,log;
text = GameObject.Find("TextPE").GetComponent<Text>();
int count = int.Parse(text.text) + 1;
text.text= count.ToString();
log = GameObject.Find("console").GetComponent<Text>();
var fileInfo = new System.IO.FileInfo("Chegou.png");
var fileSize = (fileInfo.Length/1024f)/1024f;
log.text = log.text + "[ "+ DateTime.Now.ToString("HH:mm:ss") +" ] Mensagem Enviada Work Queue : " + fileSize.ToString("0.00") + " MB" + "\n";
connection.Close();
//Console.WriteLine(" [x] Sent {0}", message);
}
}
示例14: Worker
public void Worker()
{
Text log = GameObject.Find("console").GetComponent<Text>();
var factory = new ConnectionFactory() { HostName = "diablo", UserName = "guest" ,Password = "guest"};
using(var connection = factory.CreateConnection())
using(var channel = connection.CreateModel())
{
channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false);
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
//var body = ea.Body;
//var message = Encoding.UTF8.GetString(body);
//Console.WriteLine(" [x] Received {0}", message);
/// int dots = message.Split('.').Length - 1;
//
Thread.Sleep(1000);
log.text = log.text + "[ "+ DateTime.Now.ToString("HH:mm:ss") +" ] recebendo mensagens. \n";
channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
};
channel.BasicConsume(queue: "Work_Queue",
noAck: false,
consumer: consumer);
}
}
示例15: CreateConnection
private IConnection CreateConnection()
{
IConnectionFactory factory = new ConnectionFactory(jmsServer);
IConnection connection = factory.CreateConnection(userId, password);
connection.Start();
return connection;
}