我正在尝试将Thrift与ActiveMQ集成到c#中。我的发布者和订阅者消息正在被正确地序列化和反序列化。但是在最后一步,我得到了例如“远程端已关闭”在TJSONProtocol下。这是以下一行,它会重新定义错误。
///<summary>
/// Return and consume the next byte to be Read, either taking it from the
/// data buffer if present or getting it from the transport otherwise.
///</summary>
public byte Read()
{
if (hasData)
{
hasData = false;
}
else
{
proto.trans.ReadAll(data, 0, 1);
}
return data[0];
}
这是我服务器端的自定义传输
class ThriftJMSServerTransport : TServerTransport
{
Listener listener = new Listener();
protected override TTransport AcceptImpl()
{
try
{
return new ActiveMQTransport().Accept();
// return listener.Accept(); ;
}
catch (Exception ex)
{
return null;
}
}
public override void Close()
{
throw new NotImplementedException();
}
public override void Listen()
{
// listener.Initialize();
listener.Intitializelistner();
// throw new NotImplementedException();
}
}
ActiveMQTransport Class
public delegate void MessageReceivedEventHandler(string message, string correlationId);
public class ActiveMQTransport : TTransport
{
private MemoryStream _inputStream;
private MemoryStream _outputStream;
private ActiveMQServerUtil _activeMq;
private string _corelationId;
/// <summary>
/// constructor
/// </summary>
/// <param name="activeMq"></param>
public ActiveMQTransport()
: this(new MemoryStream(), new MemoryStream())
{
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="inputStream"></param>
/// <param name="outputstream"></param>
/// <param name="activeMq"></param>
public ActiveMQTransport(MemoryStream inputStream, MemoryStream outputstream)
{
_inputStream = inputStream;
_outputStream = outputstream;
_activeMq = ActiveMQServerUtil.GetActiveMQServerUtil();
_activeMq.MessageReceived += new MessageReceivedEventHandler(_activeMq_MessageReceived);
}
public ActiveMQTransport Accept()
{
IMessage message = _activeMq.Receive();
ITextMessage textMessage = message as ITextMessage;
_activeMq_MessageReceived(textMessage.Text, textMessage.NMSCorrelationID);
return this;
}
/// <summary>
/// Read Message and write onto transport input stream
/// </summary>
/// <param name="message"></param>
/// <param name="correlationId"></param>
private void _activeMq_MessageReceived(string message, string correlationId)
{
byte[] opMessage = Encoding.UTF8.GetBytes(message);
_corelationId = correlationId;
//_inputStream = new MemoryStream();
_inputStream.Write(opMessage, 0, message.Length);
_inputStream.Position = 0;
Console.WriteLine("CorelationId: {0}, Listner Message: {1}"
, correlationId, message);
}
public override bool IsOpen
{
get { return true; }
}
public override void Open()
{
}
public override void Close()
{
}
public override int Read(byte[] buf, int off, int len)
{
try
{
int i= _inputStream.Read(buf, off, len);
return i;
}
catch(Exception ex)
{
return 0;
}
}
public override void Write(byte[] buf, int off, int len)
{
_outputStream.Write(buf, off, len);
}
/// <summary>
/// Flush result to ActiveMQ
/// </summary>
public override void Flush()
{
_activeMq.Send(Encoding.UTF8.GetString(_outputStream.ToArray()), this._corelationId);
_outputStream = new MemoryStream();
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
}
}
}
Listner类
public class Listener
{
public const string DESTINATION = "Topic1to2";
ActiveMQTransport activeMq = new ActiveMQTransport();
public static Queue<IMessage> Processqueue = new Queue<IMessage>();
public Listener()
{
}
private void PutMessageinQueue(ITextMessage message)
{
if (!string.IsNullOrEmpty(message.Text))
{
Processqueue.Enqueue(message);
}
}
private void consumer_Listener(IMessage message)
{
ITextMessage textMessage = message as ITextMessage;
Console.WriteLine(textMessage.Text);
if (!string.IsNullOrEmpty(textMessage.Text))
{
Processqueue.Enqueue(textMessage);
}
}
public void Intitializelistner()
{
IConnectionFactory factory = new
ConnectionFactory("activemq:tcp://localhost:61616/");
//Create the connection
using (IConnection connection =
factory.CreateConnection())
{
// connection.ClientId = "testing listener";
connection.Start();
//Create the Session
using (ISession session = connection.CreateSession())
{
//Create the Consumer
IMessageConsumer consumer = session.CreateConsumer(
new Apache.NMS.ActiveMQ.Commands.ActiveMQTopic(
DESTINATION));
consumer.Listener += new MessageListener(
consumer_Listener);
Console.ReadLine();
}
}
}
//static void consumer_Listener1(IMessage message)
//{
// Console.WriteLine("Receive: " +
// ((ITextMessage)message).Text);
//}
}
发布商类
public class Publisher : BaseClass
{
public const string DESTINATION = "Topic2to1";
public Publisher()
{
}
public void SendMessage(string message)
{
IConnectionFactory factory = new ConnectionFactory("activemq:tcp://localhost:61616/");
using (IConnection connection = factory.CreateConnection())
{
//Create the Session
using (ISession session = connection.CreateSession())
{
//Create the Producer for the topic/queue
IMessageProducer prod = session.CreateProducer(new ActiveMQTopic(DESTINATION));
//Send Messages
// int i = 0;
// while (!Console.KeyAvailable)
{
ITextMessage msg = prod.CreateTextMessage();
msg.Text = message;
Console.WriteLine("Sending: " + msg.Text);
prod.Send(msg);
System.Threading.Thread.Sleep(250);
// i++;
}
}
}
Console.ReadLine();
}
}
以下是客户端自定义传输
public class ThriftJmsTransport : TTransport
{
public delegate void MessageReceivedEventHandler(string message);
protected MemoryStream inputStream=new MemoryStream();
protected MemoryStream outputStream=new MemoryStream();
Listener listener = new Listener();
public ThriftJmsTransport()
{
}
public ThriftJmsTransport(MemoryStream inputStream, MemoryStream outputStream)
{
this.inputStream = inputStream;
this.outputStream = outputStream;
}
public Stream OutputStream
{
get { return outputStream; }
}
public Stream InputStream
{
get { return inputStream; }
}
public override void Close()
{
if (inputStream != null)
{
inputStream.Close();
inputStream = null;
}
if (outputStream != null)
{
outputStream.Close();
outputStream = null;
}
}
public override bool IsOpen
{
get { return true; }
}
public override void Open()
{
// listener.Initialize();
listener.Intitializelistner();
// Listener.MessageReceived += new MessageReceivedEventHandler(_activeMq_MessageReceived);
}
void _activeMq_MessageReceived(string message)
{
inputStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(message));
Console.WriteLine(message);
inputStream.Position = 0;
// _signal.Set();
}
public override int Read(byte[] buf, int off, int len)
{
if (inputStream == null)
{
throw new TTransportException(TTransportException.ExceptionType.NotOpen, "Cannot read from null inputstream");
}
return inputStream.Read(buf, off, len);
}
public override void Write(byte[] buf, int off, int len)
{
if (!outputStream.CanWrite)
outputStream = new MemoryStream();
outputStream.Write(buf, off, len);
}
public override void Flush()
{
//if (outputStream == null)
//{
// throw new TTransportException(TTransportException.ExceptionType.NotOpen, "Cannot flush null outputstream");
//}
if (outputStream != null)
{
// StreamReader reader = new StreamReader(outputStream);
string text = System.Text.Encoding.Default.GetString(outputStream.ToArray());
Send(text);
}
}
public void Send(string message)
{
var publisher=new Publisher();
publisher.SendMessage(message);
}
#region " IDisposable Support "
private bool _IsDisposed;
// IDisposable
protected override void Dispose(bool disposing)
{
if (!_IsDisposed)
{
if (disposing)
{
if (InputStream != null)
InputStream.Dispose();
if (OutputStream != null)
OutputStream.Dispose();
}
}
_IsDisposed = true;
}
#endregion
}