/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using Apache.NMS.Util; namespace Apache.NMS.EMS { /// /// Represents a NMS connection to TIBCO. /// /// public class Connection : Apache.NMS.IConnection { private Apache.NMS.AcknowledgementMode acknowledgementMode; public readonly TIBCO.EMS.Connection tibcoConnection; private IRedeliveryPolicy redeliveryPolicy; private ConnectionMetaData metaData = null; private readonly Atomic started = new Atomic(false); private bool closed = false; private bool disposed = false; public Connection(TIBCO.EMS.Connection cnx) { this.tibcoConnection = cnx; this.tibcoConnection.ExceptionHandler += this.HandleTibcoException; } ~Connection() { Dispose(false); } #region IStartable Members /// /// Starts message delivery for this connection. /// public void Start() { if(started.CompareAndSet(false, true)) { try { this.tibcoConnection.Start(); } catch(Exception ex) { ExceptionUtil.WrapAndThrowNMSException(ex); } } } public bool IsStarted { get { return this.started.Value; } } #endregion #region IStoppable Members /// /// Stop message delivery for this connection. /// public void Stop() { try { if(started.CompareAndSet(true, false)) { this.tibcoConnection.Stop(); } } catch(Exception ex) { ExceptionUtil.WrapAndThrowNMSException(ex); } } #endregion #region IConnection Members /// /// Creates a new session to work on this connection /// public Apache.NMS.ISession CreateSession() { return CreateSession(acknowledgementMode); } /// /// Creates a new session to work on this connection /// public Apache.NMS.ISession CreateSession(Apache.NMS.AcknowledgementMode mode) { try { bool isTransacted = (Apache.NMS.AcknowledgementMode.Transactional == mode); return EMSConvert.ToNMSSession(this.tibcoConnection.CreateSession(isTransacted, EMSConvert.ToSessionMode(mode))); } catch(Exception ex) { ExceptionUtil.WrapAndThrowNMSException(ex); return null; } } public void Close() { lock(this) { if(closed) { return; } try { this.tibcoConnection.ExceptionHandler -= this.HandleTibcoException; this.tibcoConnection.Stop(); this.tibcoConnection.Close(); } catch(Exception ex) { ExceptionUtil.WrapAndThrowNMSException(ex); } finally { closed = true; } } } public void PurgeTempDestinations() { } #endregion #region IDisposable Members public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected void Dispose(bool disposing) { if(disposed) { return; } if(disposing) { // Dispose managed code here. } try { Close(); } catch { // Ignore errors. } disposed = true; } #endregion #region Attributes /// /// The default timeout for network requests. /// public TimeSpan RequestTimeout { get { return Apache.NMS.NMSConstants.defaultRequestTimeout; } set { } } public Apache.NMS.AcknowledgementMode AcknowledgementMode { get { return acknowledgementMode; } set { acknowledgementMode = value; } } public string ClientId { get { try { return this.tibcoConnection.ClientID; } catch(Exception ex) { ExceptionUtil.WrapAndThrowNMSException(ex); return null; } } set { try { this.tibcoConnection.ClientID = value; } catch(Exception ex) { ExceptionUtil.WrapAndThrowNMSException(ex); } } } /// /// Get/or set the redelivery policy for this connection. /// public IRedeliveryPolicy RedeliveryPolicy { get { return this.redeliveryPolicy; } set { this.redeliveryPolicy = value; } } /// /// Gets the Meta Data for the NMS Connection instance. /// public IConnectionMetaData MetaData { get { return this.metaData ?? (this.metaData = new ConnectionMetaData()); } } #endregion /// /// A delegate that can receive transport level exceptions. /// public event ExceptionListener ExceptionListener; /// /// An asynchronous listener that is notified when a Fault tolerant connection /// has been interrupted. /// public event ConnectionInterruptedListener ConnectionInterruptedListener; /// /// An asynchronous listener that is notified when a Fault tolerant connection /// has been resumed. /// public event ConnectionResumedListener ConnectionResumedListener; private void HandleTibcoException(object sender, TIBCO.EMS.EMSExceptionEventArgs arg) { if(ExceptionListener != null) { ExceptionListener(arg.Exception); } else { Apache.NMS.Tracer.Error(arg.Exception); } } private void HandleTransportInterrupted() { Tracer.Debug("Transport has been Interrupted."); if(this.ConnectionInterruptedListener != null && !this.closed) { try { this.ConnectionInterruptedListener(); } catch { } } } private void HandleTransportResumed() { Tracer.Debug("Transport has resumed normal operation."); if(this.ConnectionResumedListener != null && !this.closed) { try { this.ConnectionResumedListener(); } catch { } } } private ConsumerTransformerDelegate consumerTransformer; /// /// A Delegate that is called each time a Message is dispatched to allow the client to do /// any necessary transformations on the received message before it is delivered. The /// ConnectionFactory sets the provided delegate instance on each Connection instance that /// is created from this factory, each connection in turn passes the delegate along to each /// Session it creates which then passes that along to the Consumers it creates. /// public ConsumerTransformerDelegate ConsumerTransformer { get { return this.consumerTransformer; } set { this.consumerTransformer = value; } } private ProducerTransformerDelegate producerTransformer; /// /// A delegate that is called each time a Message is sent from this Producer which allows /// the application to perform any needed transformations on the Message before it is sent. /// The ConnectionFactory sets the provided delegate instance on each Connection instance that /// is created from this factory, each connection in turn passes the delegate along to each /// Session it creates which then passes that along to the Producers it creates. /// public ProducerTransformerDelegate ProducerTransformer { get { return this.producerTransformer; } set { this.producerTransformer = value; } } } }