tags:

views:

73

answers:

1

I'm using Spring.NET to connect to ActiveMQ and do some fairly simple pub sub routing. Everything works fine when my selector is a simple expression like Car='Honda' but if I try a compound expression like Car='Honda' AND Make='Pilot' I never get any matches on my subscription.

Here's the code to generate the subscription, does anyone see where I might be doing something wrong?

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;
            }
        }

And then the code to send the message, which seems simple enough to me

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: Update

Ok, found the problem

bytesMessage.Properties.SetString(key, messageToSend.MessageProperties[key]);

This justs sets a single property, so my messages are only being tagged with a single property, hence the combo subscription never gets hit. Anyone know how to add more properties? You'd think bytesMessage.Properties would have a Add method, but it doesn't.

A: 

Ok, so here's what ended up happening(should it ever happen to someone else)

Spring.NET uses the Apache.NMS.ActiveMQ.MessagePropertyHelper class to handle adding properties.

Unknown to me, there is some reflection going on there is some reflection going on in there. Somewhere during my testing of my little app I got sick and tired of typing the property name "MessageType", so I just shortened it to "Type".

I can't quite figure out where it goes wrong,but clearly Type is a reserved word and just doesn't work, nor throw any kind of exception.

Jonathan Beerhalter