我正在使用 Spring.NET 连接到 ActiveMQ 并执行一些相当简单的 pub sub 路由。当我的选择器是像 Car='Honda' 这样的简单表达式时,一切正常,但如果我尝试像 Car='Honda' AND Make='Pilot' 这样的复合表达式,我的订阅永远不会得到任何匹配。
这是生成订阅的代码,有没有人看到我可能做错了什么?
public bool AddSubscription(string topicName, Dictionary<string,string> selectorList, GDException exp)
{
try
{
ActiveMQTopic topic = new ActiveMQTopic(topicName);
string selectorString = "";
if (selectorList.Keys.Count == 0)
{
// Select all items for this topic
selectorString = "2>1";
}
else
{
foreach (string key in selectorList.Keys)
{
selectorString += key + " = '" + selectorList[key] + "'" + " AND ";
}
selectorString = selectorString.Remove(selectorString.Length - 5, 5);
}
IMessageConsumer consumer = this._subSession.CreateConsumer(topic, selectorString, false);
if (consumer != null)
{
_consumers.Add(consumer);
consumer.Listener += new MessageListener(HandleRecieveMessage);
return true;
}
else
{
exp.SetValues("Error adding subscription, null consumer returned");
return false;
}
}
catch (Exception ex)
{
exp.SetValues(ex);
return false;
}
}
然后是发送消息的代码,这对我来说似乎很简单
public void SendMessage(GDPubSubMessage messageToSend)
{
if (!this.isDisposed)
{
if (_producers.ContainsKey(messageToSend.Topic))
{
IBytesMessage bytesMessage = this._pubSession.CreateBytesMessage(messageToSend.Payload);
foreach (string key in messageToSend.MessageProperties.Keys)
{
bytesMessage.Properties.SetString(key, messageToSend.MessageProperties[key]);
}
_producers[messageToSend.Topic].Send(bytesMessage, false, (byte)255, TimeSpan.FromSeconds(1));
}
else
{
ActiveMQTopic topic = new ActiveMQTopic(messageToSend.Topic);
_producers.Add(messageToSend.Topic, this._pubSession.CreateProducer(topic));
IBytesMessage bytesMessage = this._pubSession.CreateBytesMessage(messageToSend.Payload);
foreach (string key in messageToSend.MessageProperties.Keys)
{
bytesMessage.Properties.SetString(key, messageToSend.MessageProperties[key]);
}
_producers[messageToSend.Topic].Send(bytesMessage);
}
}
else
{
throw new ObjectDisposedException(this.GetType().FullName);
}
}
07/102009:更新
好的,找到问题了
bytesMessage.Properties.SetString(key, messageToSend.MessageProperties[key]);
这只是设置了一个属性,所以我的消息只被标记了一个属性,因此组合订阅永远不会被击中。有人知道如何添加更多属性吗?你会认为 bytesMessage.Properties 会有一个 Add 方法,但它没有。