static async Task Main(string[] args) { await Task.Run(async() => { while (true) { Console.WriteLine("Waiting for 10s..."); await Task.Delay(TimeSpan.FromSeconds(10)); try { Uri connecturi = new Uri(UriString); Console.WriteLine("About to connect to " + connecturi); SslTransportFactory ssl = new SslTransportFactory(); ssl.ClientCertSubject = "client"; ssl.ClientCertPassword = "******"; ssl.ClientCertFilename = "client.p12"; ssl.BrokerCertFilename = "activemq_cert"; ssl.SslProtocol = "Tls12"; //protocol, check which is using in AMQ version ITransport transport = ssl.CreateTransport(connecturi); using (IConnection connection = new Connection(connecturi, transport, new IdGenerator())) using (ISession session = connection.CreateSession()) { IDestination destination = SessionUtil.GetDestination(session, "queue://FOO.BAR"); Console.WriteLine("Using destination: " + destination); // Create a producer using (IMessageProducer producer = session.CreateProducer(destination)) { // Start the connection so that messages will be processed. connection.Start(); producer.DeliveryMode = MsgDeliveryMode.Persistent; var count = 1; while (true) { // Send a message ITextMessage request = session.CreateTextMessage($"Hello World {count++}!"); request.NMSCorrelationID = "abc"; request.Properties["NMSXGroupID"] = "cheese"; request.Properties["myHeader"] = "Cheddar"; producer.Send(request); Console.WriteLine("Message sent."); Console.WriteLine("Waiting for 10s..."); await Task.Delay(TimeSpan.FromSeconds(10)); } } } } catch (Exception e) { Console.WriteLine(e.ToString()); } } }); }
/// <summary> /// Create a transport factory for the scheme. If we do not support the transport protocol, /// an NMSConnectionException will be thrown. /// </summary> /// <param name="location"></param> /// <returns></returns> private static ITransportFactory CreateTransportFactory(Uri location) { string scheme = location.Scheme; if (null == scheme || 0 == scheme.Length) { throw new NMSConnectionException(String.Format("Transport scheme invalid: [{0}]", location.ToString())); } ITransportFactory factory = null; try { switch (scheme.ToLower()) { case "tcp": factory = new TcpTransportFactory(); break; case "ssl": factory = new SslTransportFactory(); break; case "discovery": factory = new DiscoveryTransportFactory(); break; case "failover": factory = new FailoverTransportFactory(); break; case "mock": factory = new MockTransportFactory(); break; default: throw new NMSConnectionException(String.Format("The transport {0} is not supported.", scheme)); } } catch (NMSConnectionException) { throw; } catch { throw new NMSConnectionException("Error creating transport."); } if (null == factory) { throw new NMSConnectionException("Unable to create a transport."); } return(factory); }
public Connection ConnectionActiveMq() { //Url Amazon to connect to ActiveMQ Uri connecturi = new Uri(Configuration["ActiveMQ:UrlActiveMq"]); //Create transport SSL with protocol TLS ITransportFactory sslTransportFactory = new SslTransportFactory { SslProtocol = "TLS" }; ITransport transport = sslTransportFactory.CreateTransport(connecturi); return(new Connection(connecturi, transport, new IdGenerator()) { UserName = Configuration["ActiveMQ:UserActiveMq"], Password = Configuration["ActiveMQ:PasswordActiveMq"] }); }
/// <summary> /// Create a transport factory for the scheme. /// If we do not support the transport protocol, an StompConnectionException will be thrown. /// </summary> /// <param name="location">An URI.</param> /// <returns>Returns a <see cref="ITransportFactory" />.</returns> private ITransportFactory CreateTransportFactory(Uri location) { if (location.Scheme.IsEmpty()) { throw new StompConnectionException($"Transport scheme invalid: [{location}]"); } ITransportFactory factory; try { switch (location.Scheme.ToLower()) { case "tcp": factory = new TcpTransportFactory(_stompConnectionSettings); break; case "ssl": factory = new SslTransportFactory(_stompConnectionSettings); break; default: throw new StompConnectionException($"The transport {location.Scheme} is not supported."); } } catch (StompConnectionException) { throw; } catch (Exception ex) { throw new StompConnectionException("Error creating transport.", ex); } if (null == factory) { throw new StompConnectionException("Unable to create a transport."); } return(factory); }
public IConnection CreateConnection() { ITransport transport; if (UseSsl) { var sslTransportFactory = new SslTransportFactory { SslProtocol = "Tls" }; transport = sslTransportFactory.CreateTransport(BrokerAddress); } else { transport = TransportFactory.CreateTransport(BrokerAddress); } return(new Connection(BrokerAddress, transport, new IdGenerator()) { UserName = Username, Password = Password }); }