activemq-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jim Gomes <jgo...@apache.org>
Subject Re: C#+EmbeddedBroker
Date Tue, 28 Feb 2017 21:31:10 GMT
You can use IKVM (https://www.ikvm.net/) to create a C# assembly from the
ActiveMQ jar file.  Here's the command-line you can use to compile it:

ikvmc.exe activemq-all-*.jar -out:ActiveMQ.Broker.dll -platform:anycpu
-target:library

Once you have created that assembly, create a new project that references
the ActiveMQ.Broker.dll and the IKVM.OpenJDK.Core assemblies.  Call this
new project *ActiveMQ.Embedded*. It will output the assembly
ActiveMQ.Embedded.dll.  This project will have the following source file
(name the source file AMQBroker.cs):

using System;
using System.IO;
using System.Reflection;

namespace ActiveMQ.Embedded
{
public class AMQBroker : IDisposable
{
private readonly org.apache.activemq.broker.BrokerService amqBroker;
private readonly string tmpDataDir = null;

public AMQBroker()
: this(0, false, null)
{
}

public AMQBroker(ushort port)
: this(port, false, null)
{
}

public AMQBroker(ushort port, bool persistent)
: this(port, persistent, null)
{
}

public AMQBroker(ushort port, bool persistent, string dataDirectory)
{
amqBroker = new org.apache.activemq.broker.BrokerService();
amqBroker.setPersistent(persistent);
amqBroker.setUseJmx(false);
amqBroker.setEnableStatistics(false);
amqBroker.setStartAsync(false);

if(string.IsNullOrWhiteSpace(dataDirectory))
{
tmpDataDir = GetTemporaryDirectory();
amqBroker.setDataDirectory(tmpDataDir);
}
else
{
amqBroker.setDataDirectory(dataDirectory);
}

string localserveraddress = IsLinux ? "0.0.0.0" : "localhost";

org.apache.activemq.broker.TransportConnector connector =
amqBroker.addConnector($"tcp://{localserveraddress}:{port}");
ConnectionUri = new Uri(connector.getUri().ToString());
amqBroker.start();
}

public static bool IsLinux
{
get
{
int p = (int) Environment.OSVersion.Platform;
return (p == 4) || (p == 6) || (p == 128);
}
}

public Uri ConnectionUri { get; private set; }
public int ConnectionPort { get { return ConnectionUri.Port; } }
public Uri FailoverUri { get { return new
Uri($"activemq:failover:{ConnectionUri.ToString()}"); } }

private static string GetTemporaryDirectory()
{
string tempFolder = Path.GetTempFileName();
File.Delete(tempFolder);
Directory.CreateDirectory(tempFolder);
return tempFolder;
}

public void Dispose()
{
try
{
amqBroker.stop();
if(null != tmpDataDir)
{
Directory.Delete(tmpDataDir, true);
}
}
catch(Exception e)
{
}
}
}
}

Once you have created the ActiveMQ.Embedded.dll, you can then reference it
in your project and instantiate an embedded broker as follows:

using ActiveMQ.Embedded;

AMQBroker broker = new AMQBroker(56500);  // Set whatever port number you
want to use, or leave it to the default.

It's that simple. Just remember to Dispose() it when you are done. Having
an embedded broker is fantastic for unit testing, or for many other uses.

Good luck!

-Jim


On Tue, Feb 28, 2017 at 12:00 PM xinchangan <610325092@qq.com> wrote:

I need to build an ActiveMQ client with C#.In order to improve the
efficiency
of transmit I have to use the embedded-broker within my C# Client. Finding
through all the documents and examples provided by ActiveMQ, I could not get
any clue or answers.

How can I do it? Anyone has a suggestion?

Thanks!



--
View this message in context:
http://activemq.2283324.n4.nabble.com/C-EmbeddedBroker-tp4722568.html
Sent from the ActiveMQ - Dev mailing list archive at Nabble.com.

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message