mirror of
https://github.com/apache/activemq.git
synced 2025-02-09 11:35:36 +00:00
added support for exceptions and for IConnectionFactory
git-svn-id: https://svn.apache.org/repos/asf/incubator/activemq/trunk@367642 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
2ba813b311
commit
a47deab320
17
openwire-dotnet/src/OpenWire.Client/BrokerException.cs
Executable file
17
openwire-dotnet/src/OpenWire.Client/BrokerException.cs
Executable file
@ -0,0 +1,17 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections;
|
||||||
|
using OpenWire.Client.Commands;
|
||||||
|
using OpenWire.Client.Core;
|
||||||
|
|
||||||
|
namespace OpenWire.Client {
|
||||||
|
/// <summary>
|
||||||
|
/// Exception thrown when the broker returns an error
|
||||||
|
/// </summary>
|
||||||
|
public class BrokerException : OpenWireException {
|
||||||
|
public BrokerException(BrokerError cause) : base("The operation failed: Type: "
|
||||||
|
+ cause.ExceptionClass
|
||||||
|
+ " stack: "
|
||||||
|
+ cause.StackTrace) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -19,7 +19,7 @@ namespace OpenWire.Client.Commands
|
|||||||
{
|
{
|
||||||
public const byte ID_ConnectionError = 16;
|
public const byte ID_ConnectionError = 16;
|
||||||
|
|
||||||
byte[] exception;
|
BrokerError exception;
|
||||||
ConnectionId connectionId;
|
ConnectionId connectionId;
|
||||||
|
|
||||||
|
|
||||||
@ -36,7 +36,7 @@ namespace OpenWire.Client.Commands
|
|||||||
|
|
||||||
// Properties
|
// Properties
|
||||||
|
|
||||||
public byte[] Exception
|
public BrokerError Exception
|
||||||
{
|
{
|
||||||
get { return exception; }
|
get { return exception; }
|
||||||
set { this.exception = value; }
|
set { this.exception = value; }
|
||||||
|
@ -19,7 +19,7 @@ namespace OpenWire.Client.Commands
|
|||||||
{
|
{
|
||||||
public const byte ID_ExceptionResponse = 31;
|
public const byte ID_ExceptionResponse = 31;
|
||||||
|
|
||||||
byte[] exception;
|
BrokerError exception;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -35,7 +35,7 @@ namespace OpenWire.Client.Commands
|
|||||||
|
|
||||||
// Properties
|
// Properties
|
||||||
|
|
||||||
public byte[] Exception
|
public BrokerError Exception
|
||||||
{
|
{
|
||||||
get { return exception; }
|
get { return exception; }
|
||||||
set { this.exception = value; }
|
set { this.exception = value; }
|
||||||
|
@ -13,6 +13,7 @@ namespace OpenWire.Client {
|
|||||||
private Transport transport;
|
private Transport transport;
|
||||||
IList sessions = new ArrayList();
|
IList sessions = new ArrayList();
|
||||||
private bool transacted;
|
private bool transacted;
|
||||||
|
private bool connected;
|
||||||
private bool closed;
|
private bool closed;
|
||||||
private AcknowledgementMode acknowledgementMode;
|
private AcknowledgementMode acknowledgementMode;
|
||||||
private long sessionCounter;
|
private long sessionCounter;
|
||||||
@ -33,8 +34,9 @@ namespace OpenWire.Client {
|
|||||||
/// Creates a new session to work on this connection
|
/// Creates a new session to work on this connection
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public ISession CreateSession(bool transacted, AcknowledgementMode acknowledgementMode) {
|
public ISession CreateSession(bool transacted, AcknowledgementMode acknowledgementMode) {
|
||||||
CheckClosed();
|
CheckConnected();
|
||||||
SessionInfo info = CreateSessionInfo(transacted, acknowledgementMode);
|
SessionInfo info = CreateSessionInfo(transacted, acknowledgementMode);
|
||||||
|
SyncRequest(info);
|
||||||
Session session = new Session(this, info);
|
Session session = new Session(this, info);
|
||||||
sessions.Add(session);
|
sessions.Add(session);
|
||||||
return session;
|
return session;
|
||||||
@ -65,13 +67,23 @@ namespace OpenWire.Client {
|
|||||||
set { this.acknowledgementMode = value; }
|
set { this.acknowledgementMode = value; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public string ClientId {
|
||||||
|
get { return info.ClientId; }
|
||||||
|
set {
|
||||||
|
if (connected) {
|
||||||
|
throw new OpenWireException("You cannot change the ClientId once the Connection is connected");
|
||||||
|
}
|
||||||
|
info.ClientId = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Implementation methods
|
// Implementation methods
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Performs a synchronous request-response with the broker
|
/// Performs a synchronous request-response with the broker
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Response SyncRequest(Command command) {
|
public Response SyncRequest(Command command) {
|
||||||
CheckClosed();
|
CheckConnected();
|
||||||
Response response = Transport.Request(command);
|
Response response = Transport.Request(command);
|
||||||
if (response is ExceptionResponse) {
|
if (response is ExceptionResponse) {
|
||||||
ExceptionResponse exceptionResponse = (ExceptionResponse) response;
|
ExceptionResponse exceptionResponse = (ExceptionResponse) response;
|
||||||
@ -93,10 +105,14 @@ namespace OpenWire.Client {
|
|||||||
return answer;
|
return answer;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void CheckClosed() {
|
protected void CheckConnected() {
|
||||||
if (closed) {
|
if (closed) {
|
||||||
throw new ConnectionClosedException();
|
throw new ConnectionClosedException();
|
||||||
}
|
}
|
||||||
|
if (!connected) {
|
||||||
|
SyncRequest(info);
|
||||||
|
connected = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
77
openwire-dotnet/src/OpenWire.Client/ConnectionFactory.cs
Executable file
77
openwire-dotnet/src/OpenWire.Client/ConnectionFactory.cs
Executable file
@ -0,0 +1,77 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections;
|
||||||
|
using OpenWire.Client.Commands;
|
||||||
|
using OpenWire.Client.Core;
|
||||||
|
|
||||||
|
namespace OpenWire.Client {
|
||||||
|
/// <summary>
|
||||||
|
/// Represents a connection with a message broker
|
||||||
|
/// </summary>
|
||||||
|
public class ConnectionFactory : IConnectionFactory {
|
||||||
|
private string host = "localhost";
|
||||||
|
private int port = 61616;
|
||||||
|
private string userName;
|
||||||
|
private string password;
|
||||||
|
private string clientId;
|
||||||
|
|
||||||
|
public IConnection CreateConnection() {
|
||||||
|
return CreateConnection(userName, password);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IConnection CreateConnection(string userName, string password) {
|
||||||
|
ConnectionInfo info = CreateConnectionInfo(userName, password);
|
||||||
|
Transport transport = CreateTransport();
|
||||||
|
Connection connection = new Connection(transport, info);
|
||||||
|
connection.ClientId = clientId;
|
||||||
|
return connection;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Properties
|
||||||
|
|
||||||
|
public string Host {
|
||||||
|
get { return host; }
|
||||||
|
set { host = value; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public int Port {
|
||||||
|
get { return port; }
|
||||||
|
set { port = value; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public string UserName {
|
||||||
|
get { return userName; }
|
||||||
|
set { userName = value; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Password {
|
||||||
|
get { return password; }
|
||||||
|
set { password = value; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public string ClientId {
|
||||||
|
get { return clientId; }
|
||||||
|
set { clientId = value; }
|
||||||
|
}
|
||||||
|
|
||||||
|
// Implementation methods
|
||||||
|
|
||||||
|
protected ConnectionInfo CreateConnectionInfo(string userName, string password) {
|
||||||
|
ConnectionInfo answer = new ConnectionInfo();
|
||||||
|
ConnectionId connectionId = new ConnectionId();
|
||||||
|
connectionId.Value = CreateNewConnectionID();
|
||||||
|
answer.ConnectionId = connectionId;
|
||||||
|
answer.UserName = userName;
|
||||||
|
answer.Password = password;
|
||||||
|
return answer;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected string CreateNewConnectionID() {
|
||||||
|
return Guid.NewGuid().ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Transport CreateTransport() {
|
||||||
|
// TODO
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -28,6 +28,18 @@ namespace OpenWire.Client.Core {
|
|||||||
// empty body to allow generated code to call base method
|
// empty body to allow generated code to call base method
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected virtual BrokerError ReadBrokerError(BinaryReader dataIn) {
|
||||||
|
BrokerError answer = new BrokerError();
|
||||||
|
answer.ExceptionClass = dataIn.ReadString();
|
||||||
|
answer.StackTrace = dataIn.ReadString();
|
||||||
|
return answer;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected virtual void WriteBrokerError(BrokerError command, BinaryWriter dataOut) {
|
||||||
|
dataOut.Write(command.ExceptionClass);
|
||||||
|
dataOut.Write(command.StackTrace);
|
||||||
|
}
|
||||||
|
|
||||||
protected virtual ActiveMQDestination ReadDestination(BinaryReader dataIn) {
|
protected virtual ActiveMQDestination ReadDestination(BinaryReader dataIn) {
|
||||||
return (ActiveMQDestination) CommandMarshallerRegistry.ReadCommand(dataIn);
|
return (ActiveMQDestination) CommandMarshallerRegistry.ReadCommand(dataIn);
|
||||||
}
|
}
|
||||||
|
22
openwire-dotnet/src/OpenWire.Client/Core/BrokerError.cs
Executable file
22
openwire-dotnet/src/OpenWire.Client/Core/BrokerError.cs
Executable file
@ -0,0 +1,22 @@
|
|||||||
|
using System;
|
||||||
|
using OpenWire.Client.Core;
|
||||||
|
|
||||||
|
namespace OpenWire.Client.Core {
|
||||||
|
/// <summary>
|
||||||
|
/// Represents an exception on the broker
|
||||||
|
/// </summary>
|
||||||
|
public class BrokerError : AbstractCommand {
|
||||||
|
private string exceptionClass;
|
||||||
|
private string stackTrace;
|
||||||
|
|
||||||
|
public string ExceptionClass {
|
||||||
|
get { return exceptionClass; }
|
||||||
|
set { exceptionClass = value; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public string StackTrace {
|
||||||
|
get { return stackTrace; }
|
||||||
|
set { stackTrace = value; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
21
openwire-dotnet/src/OpenWire.Client/IConnectionFactory.cs
Executable file
21
openwire-dotnet/src/OpenWire.Client/IConnectionFactory.cs
Executable file
@ -0,0 +1,21 @@
|
|||||||
|
using System;
|
||||||
|
using OpenWire.Client.Commands;
|
||||||
|
|
||||||
|
namespace OpenWire.Client {
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A Factory of IConnection objects
|
||||||
|
/// </summary>
|
||||||
|
public interface IConnectionFactory {
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new connection
|
||||||
|
/// </summary>
|
||||||
|
IConnection CreateConnection();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new connection with the given user name and password
|
||||||
|
/// </summary>
|
||||||
|
IConnection CreateConnection(string userName, string password);
|
||||||
|
}
|
||||||
|
}
|
@ -30,7 +30,7 @@ namespace OpenWire.Client.IO
|
|||||||
base.BuildCommand(command, dataIn);
|
base.BuildCommand(command, dataIn);
|
||||||
|
|
||||||
ConnectionError info = (ConnectionError) command;
|
ConnectionError info = (ConnectionError) command;
|
||||||
info.Exception = ReadBytes(dataIn);
|
info.Exception = ReadBrokerError(dataIn);
|
||||||
info.ConnectionId = (ConnectionId) CommandMarshallerRegistry.ConnectionIdMarshaller.ReadCommand(dataIn);
|
info.ConnectionId = (ConnectionId) CommandMarshallerRegistry.ConnectionIdMarshaller.ReadCommand(dataIn);
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -39,7 +39,7 @@ namespace OpenWire.Client.IO
|
|||||||
base.WriteCommand(command, dataOut);
|
base.WriteCommand(command, dataOut);
|
||||||
|
|
||||||
ConnectionError info = (ConnectionError) command;
|
ConnectionError info = (ConnectionError) command;
|
||||||
WriteBytes(info.Exception, dataOut);
|
WriteBrokerError(info.Exception, dataOut);
|
||||||
CommandMarshallerRegistry.ConnectionIdMarshaller.WriteCommand(info.ConnectionId, dataOut);
|
CommandMarshallerRegistry.ConnectionIdMarshaller.WriteCommand(info.ConnectionId, dataOut);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -30,7 +30,7 @@ namespace OpenWire.Client.IO
|
|||||||
base.BuildCommand(command, dataIn);
|
base.BuildCommand(command, dataIn);
|
||||||
|
|
||||||
ExceptionResponse info = (ExceptionResponse) command;
|
ExceptionResponse info = (ExceptionResponse) command;
|
||||||
info.Exception = ReadBytes(dataIn);
|
info.Exception = ReadBrokerError(dataIn);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -38,7 +38,7 @@ namespace OpenWire.Client.IO
|
|||||||
base.WriteCommand(command, dataOut);
|
base.WriteCommand(command, dataOut);
|
||||||
|
|
||||||
ExceptionResponse info = (ExceptionResponse) command;
|
ExceptionResponse info = (ExceptionResponse) command;
|
||||||
WriteBytes(info.Exception, dataOut);
|
WriteBrokerError(info.Exception, dataOut);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user