activemq-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Kyle Hodgson <ky...@trucash.com>
Subject What am I missing ...
Date Fri, 12 Jan 2007 21:57:42 GMT

I'm writing a simple windows form app that a) sends a message b) has an event
handler for receiving that message and c) can check a queue for any messages
that are yet un-handled.

For some reason, the event handler is firing the event onMessage(), but the
text of the incoming message is lost.  

    Public Sub sendOne()
        producer = session.CreateProducer(destination)
        producer.Persistent = True

        Dim message As ActiveMQTextMessage
        message = session.CreateTextMessage("w00t!")
        message.NMSCorrelationID = "abc"
        producer.Send(message)

    End Sub

(Works, and when I run this through the debugger I can see the text of the
message no problem with a breakpoint at the producer.Send line - so the text
has not been lost yet.)

    Sub New(ByVal arg1 As Form1)

        mainform = arg1
        factory = New ConnectionFactory(New Uri("tcp://10.78.78.1:61636"))
        connection = factory.CreateConnection()
        session = connection.CreateSession()
        destination = session.GetQueue("lyceum")
        consumer = session.CreateConsumer(destination)

        AddHandler consumer.Listener, AddressOf gotOne
        mainform.setStatus("listener added.")

    End Sub

This successfully adds an event handler gotOne to my class.

    Private Sub gotOne(ByVal message As NMS.IMessage)
        Dim amqMessage As ActiveMQTextMessage
        amqMessage = CType(message, ActiveMQTextMessage)
        mainform.setStatus("Got one.")
        mainform.setMessages("begin" + amqMessage.Text + "end")
    End Sub

gotOne works - I can see the "Got one." message and I see the "begin" when a
message comes in.  But the original message is gone (debugger shows the
contents of amqMessage.Text as "") and for some odd reason the word "end"
does not show up.  I put begin and end there just in case there were tab or
space characters I wasn't seeing.  I've tried amqMessage.toString, which did
show me lots of information about the message, but the text of the message
was still not present.  I was suspecting that it's the conversion from
IMessage to ActiveMQTextMessage but I have other code examples that seem to
indicate that I've done that correctly.

Any ideas?

The only weird thing about our setup I can think of is that I had to import
the Visual Studio 2005 source code of the .NET client available on this site
to Visual Studio 2003 to make it usable in our projects.  But I've
successfully used these DLL's in other projects, and the above behavior did
not exhibit itself.

Here's the full source code:

Imports ActiveMQ
Imports NMS
Imports ActiveMQ.Commands

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Dim L As Object

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents lbStatus As System.Windows.Forms.ListBox
    Friend WithEvents lbMessages As System.Windows.Forms.ListBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
        Me.lbStatus = New System.Windows.Forms.ListBox
        Me.lbMessages = New System.Windows.Forms.ListBox
        Me.Button1 = New System.Windows.Forms.Button
        Me.Button2 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'lbStatus
        '
        Me.lbStatus.Location = New System.Drawing.Point(0, 8)
        Me.lbStatus.Name = "lbStatus"
        Me.lbStatus.Size = New System.Drawing.Size(656, 173)
        Me.lbStatus.TabIndex = 0
        '
        'lbMessages
        '
        Me.lbMessages.HorizontalScrollbar = True
        Me.lbMessages.Location = New System.Drawing.Point(0, 184)
        Me.lbMessages.Name = "lbMessages"
        Me.lbMessages.Size = New System.Drawing.Size(656, 329)
        Me.lbMessages.TabIndex = 1
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(688, 48)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 2
        Me.Button1.Text = "DeQueue"
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(688, 88)
        Me.Button2.Name = "Button2"
        Me.Button2.TabIndex = 3
        Me.Button2.Text = "Send One"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(800, 517)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.lbMessages)
        Me.Controls.Add(Me.lbStatus)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

        setStatus("Initialized.")

        L = New ML(Me)

    End Sub


    Sub setStatus(ByVal message As String)

        If lbStatus.Items.Count = 100 Then lbStatus.Items.RemoveAt(99)
        lbStatus.Items.Insert(0, Now & " " & message)

    End Sub

    Sub setMessages(ByVal message As String)

        If lbMessages.Items.Count = 100 Then lbMessages.Items.RemoveAt(99)
        lbMessages.Items.Insert(0, Now & " " & message)

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

        L.goGetEm()

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
        L.sendOne()
    End Sub
End Class

Imports ActiveMQ
Imports ActiveMQ.Commands
Imports NMS
Imports System

Public Class ML

    Dim factory As IConnectionFactory
    Dim connection As IConnection
    Dim session As ISession
    Dim destination As IDestination
    Dim listener As MessageListener
    Dim mainform As Form1
    Dim consumer As IMessageConsumer
    Dim producer As IMessageProducer
    Sub New(ByVal arg1 As Form1)

        mainform = arg1
        factory = New ConnectionFactory(New Uri("tcp://10.78.78.1:61636"))
        connection = factory.CreateConnection()
        session = connection.CreateSession()
        destination = session.GetQueue("lyceum")
        consumer = session.CreateConsumer(destination)

        AddHandler consumer.Listener, AddressOf gotOne
        mainform.setStatus("listener added.")

    End Sub

    Public Sub sendOne()
        producer = session.CreateProducer(destination)
        producer.Persistent = True

        Dim message As ActiveMQTextMessage
        message = session.CreateTextMessage("w00t!")
        message.NMSCorrelationID = "abc"
        producer.Send(message)

    End Sub

    Private Sub gotOne(ByVal message As NMS.IMessage)
        Dim amqMessage As ActiveMQTextMessage
        amqMessage = CType(message, ActiveMQTextMessage)
        mainform.setStatus("Got one.")
        mainform.setMessages("begin" + amqMessage.Text + "end")
    End Sub

    Public Sub goGetEm()
        '// lets consume a message
        'ActiveMQTextMessage message = (ActiveMQTextMessage)
consumer.Receive();
        'if (message == null)
        '{
        'Console.WriteLine("No message received!");
        '}
        'Else
        '{
        'Console.WriteLine("Received message with ID:   " +
message.NMSMessageId);
        'Console.WriteLine("Received message with text: " + message.Text);
        '}
        Dim message = New ActiveMQTextMessage
        message = Me.consumer.Receive
        If message Is Nothing Then
            mainform.setStatus("No message waiting.")
        Else
            mainform.setMessages(message.text)
            Dim fileWriter As New System.IO.StreamWriter("c:\file.txt")
            fileWriter.Write(message.text)
            fileWriter.Close()
        End If


    End Sub
End Class

-- 
View this message in context: http://www.nabble.com/What-am-I-missing-...-tf2968575.html#a8307110
Sent from the ActiveMQ - User mailing list archive at Nabble.com.


Mime
View raw message