activemq-artemis/examples/jms/proton-cpp/readme.html

104 lines
4.5 KiB
HTML
Raw Normal View History

2014-11-10 11:14:12 -05:00
<html>
<head>
<title>ActiveMQ QPID cpp example</title>
2014-11-10 11:14:12 -05:00
<link rel="stylesheet" type="text/css" href="../common/common.css" />
<link rel="stylesheet" type="text/css" href="../common/prettify.css" />
<script type="text/javascript" src="../common/prettify.js"></script>
</head>
<body onload="prettyPrint()">
<h1>AMQP CPP example</h1>
<p>ActiveMQ is a multi protocol broker. It will inspect the initial handshake of clients to determine what protocol to use.</p>
<p>All you need to do is to connect a client into activemq's configured port and you should be able connect.</p>
2014-11-10 11:14:12 -05:00
<p>To run this example simply run the command <literal>mvn verify</literal>, execute the compile.sh script and start the executable called ./hello</p>
<pre>
# first make sure you have the dependencies you need to compile and run the client
# You will have to adapt this step according to your platform. Consult the <a href="http://qpid.apache.org/releases/qpid-0.30/programming/book/">qpid docs</a> for more information.
# There is a list of <a href="http://qpid.apache.org/packages.html">packages</a> you can install as well.
[proton-cpp]$ sudo yum install qpid-cpp-client-devel
# on a first window
[proton-cpp]$ mvn verify
# on a second window
# That goes without saying but you will of course need g++ (the C++ compiler) installed
[proton-cpp]$ ./compile.sh
[proton-cpp]$ ./hello
</pre>
<p>You don't need to do anything special to configure the ActiveMQ server to accept AMQP clients. </p>
<p>Just for the sake of documentation though we are setting the port of ActiveMQ on this example as 5672 which is the port qpid have by default. </p>
<p>This is totally optional and you don't need to follow this convention. You can use any port you chose including ActiveMQ's 5445 default port</p>
2014-11-10 11:14:12 -05:00
<pre class="prettyprint">
<code>
&lt;acceptor name="proton-acceptor"&gt;
&lt;factory-class&gt;org.apache.activemq.core.remoting.impl.netty.NettyAcceptorFactory&lt;/factory-class&gt;
2014-11-10 11:14:12 -05:00
&lt;param key="port" value="5672"/&gt;
&lt;/acceptor&gt;
</code>
</pre>
<h2>Example step-by-step</h2>
<p> We are using qpid cpp client on this example. There are several libraries you may chose from for AMQP. We have ellect one that we consider simple enough for users.</p>
<p> This example is based on <a href='http://qpid.apache.org/releases/qpid-0.30/messaging-api/cpp/examples/hello_world.cpp.html'>qpid's hello world example</a>.</p>
<ol>
<li>qpid-cpp-client-devel installed.</li>
<p>Assuming you are on Linux Fedora, you will need to run this following command</p>
<pre class="prettyprint">
<code>yum install qpid-cpp-client-devel</code>
</pre>
<p>Consult the <a href="http://qpid.apache.org/releases/qpid-0.30/programming/book/">qpid documentation</a>, and <a href="http://qpid.apache.org/packages.html">the required </a> packages for information on other platoforms.</p>
<li>Make the proper C++ imports</li>
<li> Create an amqp qpid 1.0 connection.</li>
<pre class="prettyprint">
<code>
std::string broker = argc > 1 ? argv[1] : "localhost:5445";
std::string address = argc > 2 ? argv[2] : "jms.queue.exampleQueue";
std::string connectionOptions = argc > 3 ? argv[3] : "{protocol:amqp1.0}"; // Look at the <a href="http://qpid.apache.org/releases/qpid-0.30/programming/book/connections.html#connection-options">docs</a> for more options
Connection connection(broker, connectionOptions);
connection.open();
</code>
</pre>
<li>Create a session</li>
<pre class="prettyprint">
<code>Session session = connection.createSession();</code>
</pre>
<li>Create a sender</li>
<pre class="prettyprint">
<code>Sender sender = session.createSender(address);</code>
</pre>
<li>send a simple message</li>
<pre class="prettyprint">
<code>sender.send(Message("Hello world!"));</code>
</pre>
<li>create a receiver</li>
<pre class="prettyprint">
<code>Receiver receiver = session.createReceiver(address);</code>
</pre>
<li>receive the simple message</li>
<pre class="prettyprint">
<code>Message message = receiver.fetch(Duration::SECOND * 1);
</code>
</pre>
<li>acknowledge the message</li>
<pre class="prettyprint">
<code>session.acknowledge();</code>
</pre>
<li>close the connection</li>
<pre class="prettyprint">
<code>connection.close();</code>
</pre>
</ol>
</body>
</html>