Currently I have ApacheMq running locally and have managed to get the following code working to read the Queue. However I don't really understand C# and the rest of my programme is created in VB. Hence I have tried ot convert it. Unfortunately it is generating errors and as I am unsure what the original C# code means, I am unsure how to correct these.
When converted to VB (with errors Underlined)
Any assistance in converting the C@ code woudl be appreciated.
Code:
Origional C# (working) code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Apache.NMS;
using Apache.NMS.ActiveMQ;
namespace Test {
class Program {
static ISession session = null;
static void Main(string[] args) {
IConnectionFactory factory = new ConnectionFactory("tcp://localhost:61616/");
IConnection connection = factory.CreateConnection();
connection.ClientId = "ItsOK";
ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge);
var topic = session.GetQueue("testq");
IMessageConsumer consumer = session.CreateConsumer(topic);
connection.Start();
consumer.Listener += new MessageListener(OnMessage);
Console.WriteLine("waiting for messages (Press ENTER to stop.)");
Console.ReadLine();
connection.Close();
private static void OnMessage(IMessage message)
{
try
{
Console.WriteLine("Message received");
ITextMessage msg = (ITextMessage)message;
message.Acknowledge();
Console.WriteLine(msg.Text);
Console.WriteLine("\n");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine("---");
Console.WriteLine(ex.InnerException);
Console.WriteLine("---");
Console.WriteLine(ex.InnerException.Message);
}
}
}
Code:
Imports Apache.NMS
Imports Apache.NMS.activemq
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim factory As IConnectionFactory = New ConnectionFactory("tcp://localhost:61616/")
Dim connection As IConnection = factory.CreateConnection()
connection.ClientId = "ItsOK"
Dim session As ISession = connection.CreateSession(AcknowledgementMode.AutoAcknowledge)
Dim topic As ISession = session.GetQueue("testq")
Dim consumer As IMessageConsumer = session.CreateConsumer(topic)
connection.Start()
consumer.Listener += New MessageListener(OnMessage) - Errors Underlined
Console.WriteLine("waiting for messages (Press ENTER to stop.)")
Console.ReadLine()
connection.Close()
End Sub
Private Sub OnMessage(ByVal IMessage)
Try
Console.WriteLine("Message received")
Dim msg As ITextMessage(Message) - errors Underlined
IMessage.Acknowledge()
Console.WriteLine(msg.Text)
Console.WriteLine("\n")
catch (Exception ex) - errors Underlined
Console.WriteLine(ex.Message)
Console.WriteLine("---")
Console.WriteLine(ex.InnerException) - errors Underlined
Console.WriteLine("---")
Console.WriteLine(ex.InnerException.Message) - errors Underlined
End Try
End Sub