async public void ConnectOrDisconnect(object sender, EventArgs eventArgs) { try { if (!_isConnected) { string uriInput = _demoPage.URI; _demoPage.Log("CONNECTING: " + uriInput); await Task.Factory.StartNew(() => { IConnectionFactory connectionFactory = new StompConnectionFactory(new Uri(uriInput)); _connection = connectionFactory.CreateConnection(null, null); _connection.ExceptionListener = new ExceptionHandler(_demoPage); _consumers = new Dictionary<String, List<IMessageConsumer>>(); _session = _connection.CreateSession(false, SessionConstants.AUTO_ACKNOWLEDGE); _connection.Start(); }); _demoPage.Log("CONNECTED"); // Enable User Interface for Connected application _demoPage.EnableUI(true); _isConnected = true; } else { _demoPage.Log("CLOSE"); if (_connection != null) { CloseConsumers(); _consumers.Clear(); _connection.Close(); } _demoPage.Log("DISCONNECTED"); _isConnected = false; _demoPage.EnableUI(false); _connection = null; } } catch(Exception exc) { if (_connection != null) { CloseConsumers(); _consumers.Clear(); _connection.Close(); } _demoPage.Log("CONNECTION FAILED: " + exc.Message); _demoPage.EnableUI(false); } }
private void Connect() { try { IConnectionFactory connectionFactory = new StompConnectionFactory(new Uri(Serverlocation)); _connection = connectionFactory.CreateConnection(); _consumers = new Dictionary <string, List <IMessageConsumer> >(); _session = _connection.CreateSession(false, SessionConstants.AUTO_ACKNOWLEDGE); _connection.Start(); SubscribeTo(_personalQueue); SubscribeTo(GlobalBroadcastTopic); } catch (Exception) { _connection?.Close(); } }
public TranslatorForm() { InitializeComponent(); try { IConnectionFactory connectionFactory = new StompConnectionFactory(new Uri(Serverlocation)); var connection = connectionFactory.CreateConnection(); _session = connection.CreateSession(false, SessionConstants.AUTO_ACKNOWLEDGE); connection.Start(); Output($"Connected to {Serverlocation}"); var destination = _session.CreateQueue("/queue/JmsTranslator"); var consumer = _session.CreateConsumer(destination); consumer.MessageListener = this; Output("Translator is ready"); } catch (Exception e) { Output("Could not connect: " + e.Message); } }
/* * "Delegate, invoked by the "click handler", which actually establishes the JMS connection. */ private void JMS_Connect() { // Immediately disable the connect button ConnectButton.Enabled = false; Log("CONNECT:" + LocationText.Text); String username = (UsernameText.Text.Length != 0) ? UsernameText.Text : null; String password = (PasswordText.Text.Length != 0) ? PasswordText.Text : null; try { IConnectionFactory connectionFactory = new StompConnectionFactory(new Uri(LocationText.Text)); connection = connectionFactory.CreateConnection(username, password); Log("CONNECTED"); connection.ExceptionListener = new ExceptionHandler(this); consumers = new Dictionary<String, List<IMessageConsumer>>(); session = connection.CreateSession(false, SessionConstants.AUTO_ACKNOWLEDGE); connection.Start(); // Enable User Interface for Connected application SubscribeButton.Enabled = true; SendButton.Enabled = true; UnsubscribeButton.Enabled = true; DisconnectButton.Enabled = true; } catch (Exception exc) { if (connection != null) { connection.Close(); } Log("CONNECTION FAILED: " + exc.Message); ConnectButton.Enabled = true; } }
/* * Button click handlers */ private void ConnectButton_Click(object sender, RoutedEventArgs e) { // Immediately disable the connect button ConnectButton.IsEnabled = false; Log("CONNECT:" + LocationText.Text); String username = (UsernameText.Text.Length != 0) ? UsernameText.Text : null; String password = (PasswordText.Password.Length != 0) ? PasswordText.Password : null; String location = LocationText.Text; // new StompConnectionFactory() MUST be called on the UI Thread IConnectionFactory connectionFactory = new StompConnectionFactory(new Uri(location)); Thread connectionThread = new Thread(new ThreadStart(() => { try { // connectionFactory.CreateConnection() MUST be called on a separate thread // to prevent blocking the UI Thread in Silverlight connection = connectionFactory.CreateConnection(username, password); connection.ExceptionListener = new ExceptionHandler(this); consumers = new Dictionary<String, List<IMessageConsumer>>(); session = connection.CreateSession(false, SessionConstants.AUTO_ACKNOWLEDGE); transactedSession = connection.CreateSession(true, SessionConstants.SESSION_TRANSACTED); connection.Start(); this.Dispatcher.BeginInvoke(() => { Log("CONNECTED"); // Enable User Interface for Connected application BeginButton.IsEnabled = true; SubscribeButton.IsEnabled = true; SendButton.IsEnabled = true; UnsubscribeButton.IsEnabled = true; CloseButton.IsEnabled = true; }); } catch (Exception exc) { this.Dispatcher.BeginInvoke(() => { ConnectButton.IsEnabled = true; Log("EXCEPTION: " + exc.GetType().Name); }); } })); connectionThread.Start(); }