private void onDataMessage(IMessage msg) { int bytesRead = 0; int zLev = msg.Properties.GetInt("zLevReply"); byte[] byteBuffer = new byte[1 << 3 * zLev]; if (msg is IBytesMessage) { IBytesMessage bMsg = (IBytesMessage)msg; bytesRead = bMsg.ReadBytes(byteBuffer); print("We are receiving a DATA message of " + bytesRead + " bytes."); NextBrickData = byteBuffer; //This sets the byte buffer of the next brick up. NextBrick = msg.Properties.GetInt("BrickNumReply"); print("This is the nextBrick number: " + NextBrick); } ByteDataDict.Add(NextBrick, nextBrickData); }
// 消息监听 void consumer_Listener(IMessage message) { if (message is ITextMessage) { ITextMessage mqMessage = message as ITextMessage; if (mqMessage != null && _onMessageHandle != null) { _onMessageHandle.BeginInvoke(mqMessage.Text, null, null); } } else if (message is IBytesMessage) { IBytesMessage mqMessage = message as IBytesMessage; if (mqMessage != null && _onMessageHandle != null) { byte[] byteContent = new byte[1024]; int length = -1; StringBuilder content = new StringBuilder(); while ((length = mqMessage.ReadBytes(byteContent)) != -1) { content.Append(Encoding.UTF8.GetString(byteContent, 0, length)); } _onMessageHandle.BeginInvoke(content.ToString(), null, null); } } }
private void Consumer_Listener1(IMessage message) { //if (message is ITextMessage) //{ // string str = ((ITextMessage)message).Text; // object obj = getSerializationInstance(senderTopic).Deserialize<object>(str); // objRecListener.OnMessage(obj); //} if (message is IBytesMessage) { IBytesMessage bms = (IBytesMessage)message; int length = (int)bms.BodyLength; if (length == 0) { // log error return; } byte[] bytes = new byte[length]; bms.ReadBytes(bytes); object obj = getSerializationInstance(receiveQueue).Deserialize(bytes); if (obj == null) { return; } objRecListener.OnMessage(obj); } else { // log error } }
public void SendReceiveForeignBytesMessageTest( [Values(MsgDeliveryMode.Persistent, MsgDeliveryMode.NonPersistent)] MsgDeliveryMode deliveryMode) { using (IConnection connection = CreateConnection()) { connection.Start(); using (ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge)) { IDestination destination = CreateDestination(session, DestinationType.Topic); using (IMessageConsumer consumer = session.CreateConsumer(destination)) using (IMessageProducer producer = session.CreateProducer(destination)) { try { producer.DeliveryMode = deliveryMode; BytesMessage request = new BytesMessage(); request.Properties[propertyName] = propertyValue; request.WriteBytes(bytesContent); producer.Send(request); IBytesMessage message = consumer.Receive(receiveTimeout) as IBytesMessage; Assert.IsNotNull(message, "No message returned!"); Assert.AreEqual(request.Properties.Count, message.Properties.Count, "Invalid number of properties."); Assert.AreEqual(deliveryMode, message.NMSDeliveryMode, "NMSDeliveryMode does not match"); // Check the body byte[] content = new byte[bytesContent.Length]; Assert.AreEqual(bytesContent.Length, message.ReadBytes(content)); Assert.AreEqual(bytesContent, content, "BytesMessage body was wrong."); // use generic API to access entries Assert.AreEqual(propertyValue, message.Properties[propertyName], "generic map entry: " + propertyName); // use type safe APIs Assert.AreEqual(propertyValue, message.Properties.GetString(propertyName), "map entry: " + propertyName); } catch (NotSupportedException) { } } } } }
private void Translate(IBytesMessage received) { try { var bytes = new byte[(int)received.BodyLength]; received.ReadBytes(bytes); var messageToSend = _session.CreateBytesMessage(); messageToSend.WriteBytes(bytes); messageToSend.SetStringProperty("author", received.GetStringProperty("author")); messageToSend.SetStringProperty("group", received.GetStringProperty("group")); messageToSend.JMSReplyTo = GetDestination(_personalQueue); var producer = _session.CreateProducer(GetDestination("/queue/JmsTranslator")); producer.Send(messageToSend); producer.Close(); } catch (Exception e) { MessageBox.Show(@"Error in Translate " + e.Message); } }
public static ISpecificRecord unpackMessage <T>(IBytesMessage message) { long len = message.BodyLength; byte[] data = new byte[(int)len]; int read = message.ReadBytes(data); MemoryStream ins = new MemoryStream(data); Avro.IO.Decoder dc = new Avro.IO.BinaryDecoder(ins); // Decode<T> r = new Decode<T>(); // SpecificReader<T> r = new SpecificReader<T>(c); SpecificDefaultReader sdr = new SpecificDefaultReader(x, z); DatumReader <T> reader = new SpecificReader <T>(sdr); T actual = reader.Read(typeof(T), dc); //T actual = r(dc); ins.Close(); return((ISpecificRecord)actual); }
private void Consumer_Listener2(IMessage message) { //if (message is ITextMessage) //{ // string str = ((ITextMessage)message).Text; // object obj = getSerializationInstance(publisherTopic).Deserialize(str); // foreach(IObjectListener listener in objSubscribers[publisherTopic]) // { // listener.OnMessage(obj); // } //} if (message is IBytesMessage) { IBytesMessage bms = (IBytesMessage)message; int length = (int)bms.BodyLength; if (length == 0) { // log error return; } byte[] bytes = new byte[length]; bms.ReadBytes(bytes); object obj = getSerializationInstance(subscribeTopic).Deserialize(bytes); if (obj == null) { return; } foreach (IObjectListener listener in objSubscribers[subscribeTopic]) { listener.OnMessage(obj); } } else { // log error } }
private object DecodeIBytesMessage(IBytesMessage msg, JSONPROXY map) { int len = (int) msg.BodyLength; byte[] bytes = new byte[len]; msg.ReadBytes(bytes, len); MemoryStream ins = new MemoryStream(bytes); Schema rs = SchemeFor(msg.ContentType, map); Schema ws = rs; if (rs != null) { var sdr = new DefaultReader(rs, ws); Avro.IO.Decoder dc = new Avro.IO.BinaryDecoder(ins); try { object actual = sdr.Read<object>(null, dc); Type t = actual.GetType(); return actual; } catch (Exception e) { Console.Error.WriteLine(e); throw; } finally { ins.Close(); } } else { Console.WriteLine("*** NO DECODER FOR msg.ContentType='{0}' FOUND ***", msg.ContentType); return null; } }
public int ReadBytes(byte[] value) { return(message.ReadBytes(value)); }