You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@activemq.apache.org by axel <ax...@gmail.com> on 2013/09/27 09:54:43 UTC

how to achieve OnMessage event in asp.net app to automatically fetch receives

Hello All,

I am newbie to use ActiveMQ MOM in my asp.net (C#) project. I am in trouble
with the following question 2 days, so I open this thread here to find you
to figure it out. Thanks in advance :)

My Question:
I have topic publisher and multiple topic subscribers. If I write the
consumer program in console application, it works really fantastic. But,
after recoded in asp.net application, it does not work. (I mean, when I sent
a topic by publisher, no responses on subscriber side.)

Here's my entire code.
//This is publisher
        protected void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                IConnectionFactory connFactory = new
ConnectionFactory("tcp://localhost:61616");
                using (IConnection connection =
connFactory.CreateConnection())
                {
                    using (ISession session = connection.CreateSession())
                    {
                        IMessageProducer producer = session.CreateProducer(
                            new ActiveMQTopic("Distribution test"));

                        ITextMessage msg = producer.CreateTextMessage();
                        msg.Text = this.txtName.Text;
                        producer.Send(msg);

                    }
                }
            }
            catch (Exception ex)
            {
                //TODO:
            }
        }

===================================================================

//This is consumer
    public delegate void ReceivedMessageDelegate(string message);
    public partial class _Default : System.Web.UI.Page
    {
        public event ReceivedMessageDelegate OnMessageReceived;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    IConnectionFactory connFactory = new
ConnectionFactory("tcp://localhost:61616");
                    using (IConnection connection =
connFactory.CreateConnection())
                    {
                        connection.ClientId = "testing listener";
                        connection.Start();

                        using (ISession session =
connection.CreateSession())
                        {
                            IMessageConsumer consumer =
session.CreateDurableConsumer(new ActiveMQTopic("Distribution test"),
"testing listener", null, false);
                            consumer.Listener += (message =>
                                {
                                    var msg = message as ITextMessage;
                                    if (msg == null) throw new
InvalidCastException();
                                    if(OnMessageReceived != null)
                                    {
                                        this.OnMessageReceived(msg.Text);
                                    }
                                });
                        }
                    }
                }
                catch (Exception ex)
                { //TODO: }
                }
            }
        }
    }

Cup,



--
View this message in context: http://activemq.2283324.n4.nabble.com/how-to-achieve-OnMessage-event-in-asp-net-app-to-automatically-fetch-receives-tp4671879.html
Sent from the ActiveMQ - Dev mailing list archive at Nabble.com.

Re: how to achieve OnMessage event in asp.net app to automatically fetch receives

Posted by axel <ax...@gmail.com>.
Thanks for your reply. My testing application does not publish onto IIS, just
on the localhost. Any example for ASP.NET and traditional web service
(asmx)?

Thanks,
Axel



--
View this message in context: http://activemq.2283324.n4.nabble.com/how-to-achieve-OnMessage-event-in-asp-net-app-to-automatically-fetch-receives-tp4671879p4671973.html
Sent from the ActiveMQ - Dev mailing list archive at Nabble.com.

Re: how to achieve OnMessage event in asp.net app to automatically fetch receives

Posted by jonpen <jo...@hotmail.com>.
I might be due to the fact the services running in IIS that a use non-http
transport protocols will need to activated "woken up" when a message arrives
for them. 

You could implement an Activator and integrated it into WAS. 

Or implement a windows service that peeks the queue and then wakes the
appropriate service in IIS by doing a HTTP request for its WSLD.

Or you could install Windows Server AppFabric and enable Autostart which
will make sure your service is always awake, although i have only used this
for WCF services but i presume it should do the same for ASP services.

I would try option three, and if that doesn't work try the secound option
and final if that doesn't work try the last option, example of how to do it
is included with the WCF-WF exacmples. Also Appfabric has some neat
features.



--
View this message in context: http://activemq.2283324.n4.nabble.com/how-to-achieve-OnMessage-event-in-asp-net-app-to-automatically-fetch-receives-tp4671879p4671958.html
Sent from the ActiveMQ - Dev mailing list archive at Nabble.com.

Re: how to achieve OnMessage event in asp.net app to automatically fetch receives

Posted by axel <ax...@gmail.com>.
Thanks for your reply. As I mentioned, message can be consumed in the console
application. (When I clicked send button, two clients receive the message
immediately.) 
In the meantime, I do not very undertand what you said "enough time" means.
Could you please explain it more or any example for ASP.NET?

Thanks,
Axel,



--
View this message in context: http://activemq.2283324.n4.nabble.com/how-to-achieve-OnMessage-event-in-asp-net-app-to-automatically-fetch-receives-tp4671879p4671972.html
Sent from the ActiveMQ - Dev mailing list archive at Nabble.com.

Re: how to achieve OnMessage event in asp.net app to automatically fetch receives

Posted by Timothy Bish <ta...@gmail.com>.
On 09/27/2013 03:54 AM, axel wrote:
> Hello All,
>
> I am newbie to use ActiveMQ MOM in my asp.net (C#) project. I am in trouble
> with the following question 2 days, so I open this thread here to find you
> to figure it out. Thanks in advance :)
>
> My Question:
> I have topic publisher and multiple topic subscribers. If I write the
> consumer program in console application, it works really fantastic. But,
> after recoded in asp.net application, it does not work. (I mean, when I sent
> a topic by publisher, no responses on subscriber side.)
>
> Here's my entire code.
> //This is publisher
>          protected void btnSend_Click(object sender, EventArgs e)
>          {
>              try
>              {
>                  IConnectionFactory connFactory = new
> ConnectionFactory("tcp://localhost:61616");
>                  using (IConnection connection =
> connFactory.CreateConnection())
>                  {
>                      using (ISession session = connection.CreateSession())
>                      {
>                          IMessageProducer producer = session.CreateProducer(
>                              new ActiveMQTopic("Distribution test"));
>
>                          ITextMessage msg = producer.CreateTextMessage();
>                          msg.Text = this.txtName.Text;
>                          producer.Send(msg);
>
>                      }
>                  }
>              }
>              catch (Exception ex)
>              {
>                  //TODO:
>              }
>          }
>
> ===================================================================
>
> //This is consumer
>      public delegate void ReceivedMessageDelegate(string message);
>      public partial class _Default : System.Web.UI.Page
>      {
>          public event ReceivedMessageDelegate OnMessageReceived;
>
>          protected void Page_Load(object sender, EventArgs e)
>          {
>              if (!IsPostBack)
>              {
>                  try
>                  {
>                      IConnectionFactory connFactory = new
> ConnectionFactory("tcp://localhost:61616");
>                      using (IConnection connection =
> connFactory.CreateConnection())
>                      {
>                          connection.ClientId = "testing listener";
>                          connection.Start();
>
>                          using (ISession session =
> connection.CreateSession())
>                          {
>                              IMessageConsumer consumer =
> session.CreateDurableConsumer(new ActiveMQTopic("Distribution test"),
> "testing listener", null, false);
>                              consumer.Listener += (message =>
>                                  {
>                                      var msg = message as ITextMessage;
>                                      if (msg == null) throw new
> InvalidCastException();
>                                      if(OnMessageReceived != null)
>                                      {
>                                          this.OnMessageReceived(msg.Text);
>                                      }
>                                  });
>                          }
>                      }
>                  }
>                  catch (Exception ex)
>                  { //TODO: }
>                  }
>              }
>          }
>      }
>
> Cup,
>
>
>
> --
> View this message in context: http://activemq.2283324.n4.nabble.com/how-to-achieve-OnMessage-event-in-asp-net-app-to-automatically-fetch-receives-tp4671879.html
> Sent from the ActiveMQ - Dev mailing list archive at Nabble.com.
>
Try using an sync receive instead of the async version, I doubt there's 
enough time there for the consumer to be dispatched a message.

-- 
Tim Bish
Sr Software Engineer | RedHat Inc.
tim.bish@redhat.com | www.fusesource.com | www.redhat.com
skype: tabish121 | twitter: @tabish121
blog: http://timbish.blogspot.com/