[prev in list] [next in list] [prev in thread] [next in thread] 

List:       activemq-dev
Subject:    Re: C#+EmbeddedBroker
From:       Jim Gomes <jgomes () apache ! org>
Date:       2017-02-28 21:31:10
Message-ID: CAGeZ+big2XiicJCEQo2Z7+dN0PD8x=StT4+mbEgOaUFuqKoTrg () mail ! gmail ! com
[Download RAW message or body]


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.


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic