| <div class="wiki-content maincontent"> |
| <p>You can use <a shape="rect" href="http://gcc.gnu.org/java/">GCJ</a> to build ActiveMQ as a shared library you can reuse from C++.</p> |
| |
| <h3>Native compile ActiveMQ HOWTO</h3> |
| |
| <h4>Abstract</h4> |
| <p>This document describes how to native compile ActiveMQ for use in a C++ environment. The version of ActiveMQ used is 3.2 in this howto. To compile you'll need GCC 4.0.2, or later, with both Java, and C/C++ support.</p> |
| |
| <h4>Tools Setup</h4> |
| <p>If you don't already have GCC 4.0.2 installed you need to download and build it. See GCC manuals for complete instructions on how to build GCC but below is a short descriptions of the steps involved. The GCC build steps assumes that you already have an older GCC compiler installed.</p> |
| |
| <ul><li>Unpack GCC into an arbitrary directory, for example /opt/gccbuild, and then create a separate output directory. Your directory structure should look similar to this; |
| <structured-macro ac:macro-id="93a6eb11-be7b-4c05-8174-292ba8661d7f" ac:name="noformat" ac:schema-version="1"><plain-text-body> |
| /opt/gccbuild/gcc-4.0.2 |
| /opt/gccbuild/output |
| </plain-text-body></structured-macro></li><li>Go to the output directory and run configure. |
| <structured-macro ac:macro-id="3c50361b-035c-4d89-8c23-36ca3362dd3a" ac:name="noformat" ac:schema-version="1"><plain-text-body> |
| cd /opt/gccbuild/output |
| ../gcc-4.0.2/configure --prefix=/opt/gcc402 |
| --enable-shared |
| --enable-threads=posix |
| --enable-languages=c,c++,java |
| </plain-text-body></structured-macro></li><li>Run make. |
| <structured-macro ac:macro-id="d567ed56-a7ef-4dd5-97ea-ce0d50cdda52" ac:name="noformat" ac:schema-version="1"><plain-text-body> |
| make bootstrap |
| make install |
| </plain-text-body></structured-macro></li></ul> |
| |
| |
| <ul><li>Download ActiveMQ and copy the JARs to a new empty directory /opt/app, including |
| <structured-macro ac:macro-id="0f658903-01d0-4f47-b9b5-96a68be84721" ac:name="noformat" ac:schema-version="1"><plain-text-body> |
| activeio-1.1.jar |
| activemq-core-3.2.jar |
| commons-logging-1.0.3.jar |
| concurrent-1.3.4.jar |
| geronimo-spec-j2ee-jacc-1.0-rc4.jar |
| geronimo-spec-j2ee-management-1.0-rc4.jar |
| geronimo-spec-jms-1.1-rc4.jar |
| geronimo-spec-jta-1.0.1B-rc4.jar |
| log4j-1.2.8.jar |
| </plain-text-body></structured-macro></li></ul> |
| |
| |
| <h4>Write the Glue Code</h4> |
| <p>Either access the ActiveMQ classes directly from C++ or write a facade object in Java that handles all startup and shutdown logic of ActiveMQ. Save the glue files in the same directory as for the ActiveMQ jars.</p> |
| |
| <p>An CNI example using a Java object starting the MQ.</p> |
| |
| <h5>Bootstrap.cpp</h5> |
| <structured-macro ac:macro-id="74a1f7b3-5e16-4ec2-a7bf-10b5c5130735" ac:name="noformat" ac:schema-version="1"><plain-text-body> |
| #include <gcj/cni.h> |
| #include <iostream> |
| #include <java/lang/System.h> |
| #include <java/lang/Throwable.h> |
| #include <java/io/PrintStream.h> |
| #include "MQAdapter.h" |
| |
| using namespace std; |
| |
| int main(int argc, char* argv[]) |
| { |
| cout << "Entering main" << endl; |
| using namespace java::lang; |
| |
| try |
| { |
| // Create and startup Java VM |
| JvCreateJavaVM(NULL) ; |
| JvAttachCurrentThread(NULL, NULL) ; |
| |
| System::out->println(JvNewStringLatin1("Java println")) ; |
| |
| // Start ActiveMQ |
| MQAdapter* pAdapter = new MQAdapter() ; |
| pAdapter->start() ; |
| |
| // Send a message |
| pAdapter->send(JvNewStringLatin1("Hello World!")) ; |
| |
| // Shutdown ActiveMQ |
| pAdapter->stop() ; |
| |
| JvDetachCurrentThread() ; |
| } |
| catch( Throwable *t ) |
| { |
| System::err->println(JvNewStringLatin1("Exception")) ; |
| t->printStackTrace() ; |
| } |
| } |
| </plain-text-body></structured-macro> |
| <h5>MQAdapter.java</h5> |
| <structured-macro ac:macro-id="2fefcabc-d370-4e1e-b1a7-94883d3d3591" ac:name="noformat" ac:schema-version="1"><plain-text-body> |
| import org.activemq.*; |
| import java.util.Hashtable ; |
| import javax.jms.*; |
| import javax.naming.*; |
| |
| public class MQAdapter |
| { |
| private InitialContext jndiContext ; |
| private QueueConnectionFactory factory ; |
| private QueueConnection connection ; |
| private QueueSession session ; |
| private QueueSender sender ; |
| private Queue queue ; |
| |
| public MQAdapter() |
| { |
| } |
| |
| public void start() |
| { |
| try |
| { |
| Hashtable props = new Hashtable() ; |
| props.put(Context.INITIAL_CONTEXT_FACTORY, "org.activemq.jndi.ActiveMQInitialContextFactory") ; |
| props.put(Context.PROVIDER_URL, "tcp://localhost:61616") ; |
| props.put("queue.MyQueue", "example.MyQueue") ; |
| |
| jndiContext = new InitialContext(props) ; |
| |
| // Create and configure JMS connection factory |
| factory = (QueueConnectionFactory)jndiContext.lookup("ConnectionFactory") ; |
| |
| // Lookup Queue |
| queue = (Queue)jndiContext.lookup("MyQueue") ; |
| |
| // Create a JMS connection |
| connection = (QueueConnection)factory.createQueueConnection() ; |
| System.out.println("Created connection: " + connection) ; |
| |
| // Create a JMS session |
| session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE) ; |
| System.out.println("Created session: " + session) ; |
| |
| // Create JMS sender |
| sender = session.createSender(queue) ; |
| } |
| catch( Exception e ) |
| { |
| e.printStackTrace() ; |
| |
| try |
| { |
| if( connection != null ) |
| connection.close() ; |
| } catch( JMSException jmse ) |
| { /* ignore */ } |
| } |
| } |
| |
| public void stop() |
| { |
| try |
| { |
| if( connection != null ) |
| connection.close() ; |
| } catch( JMSException e ) |
| { /* ignore */ } |
| } |
| |
| public void send(String msg) |
| { |
| TextMessage message ; |
| |
| try |
| { |
| message = session.createTextMessage(msg) ; |
| sender.send(message) ; |
| } |
| catch( JMSException e ) |
| { |
| e.printStackTrace() ; |
| } |
| } |
| } |
| </plain-text-body></structured-macro> |
| |
| <h4>Compile the Java and C++ Code</h4> |
| <p>The Java code must be BC compiled to be able to dynamically link required classes as needed, see reference for more information on BC compilation. Use the suggested script to compile all ActiveMQ JARs and create a class map database.</p> |
| |
| <structured-macro ac:macro-id="fa02f16f-d456-4563-950b-2c2b97495410" ac:name="info" ac:schema-version="1"><parameter ac:name="title">Note</parameter><rich-text-body> |
| <p>Using -Bsymbolic does not seem to work, use -symbolic instead.</p></rich-text-body></structured-macro> |
| |
| <structured-macro ac:macro-id="cc0d4ed4-9457-4de6-a80d-20aab110eddd" ac:name="noformat" ac:schema-version="1"><plain-text-body> |
| compile.sh: |
| |
| #!/bin/sh |
| |
| # Create new classmap database |
| gcj-dbtool -n classmap.db |
| |
| for JAR_FILE in `find -iname "*.jar"` |
| do |
| echo "Compiling ${JAR_FILE} to native" |
| gcj -shared -findirect-dispatch -fjni -fPIC -Wl,-symbolic -o ${JAR_FILE}.so ${JAR_FILE} |
| gcj-dbtool -a classmap.db ${JAR_FILE} ${JAR_FILE}.so |
| done |
| </plain-text-body></structured-macro> |
| |
| <ul><li>Run the above script and set environment property GCJ_PROPERTIES. |
| <structured-macro ac:macro-id="769e79ed-10bc-4951-99dd-ff9bb4786940" ac:name="noformat" ac:schema-version="1"><plain-text-body> |
| ./compile.sh |
| export GCJ_PROPERTIES="gnu.gcj.precompiled.db.path=/opt/app/classmap.db" |
| </plain-text-body></structured-macro></li></ul> |
| |
| |
| <ul><li>Java compile MQAdapter.java |
| <structured-macro ac:macro-id="829b8d71-c144-4232-890b-1cc6b89514a6" ac:name="noformat" ac:schema-version="1"><plain-text-body> |
| gcj --classpath=./geronimo-spec-jms-1.1-rc4.jar:./activemq-core-3.2.jar -C MQAdapter.java |
| </plain-text-body></structured-macro></li></ul> |
| |
| |
| <ul><li>Generate CNI header for MQAdapter.class |
| <structured-macro ac:macro-id="0c9263bc-06f4-4058-b810-168ce4bc66e9" ac:name="noformat" ac:schema-version="1"><plain-text-body> |
| gcjh MQAdapter |
| </plain-text-body></structured-macro></li></ul> |
| |
| |
| <ul><li>JAR the Java glue code |
| <structured-macro ac:macro-id="97d07144-63e6-4fcc-92da-bad27355c062" ac:name="noformat" ac:schema-version="1"><plain-text-body> |
| fastjar cf MQAdapter.jar MQAdapter.class |
| </plain-text-body></structured-macro></li></ul> |
| |
| |
| <ul><li>Native compile the Java JAR into a shared library, add output directory to LD_LIBRARY_PATH. |
| <structured-macro ac:macro-id="1d7dd7b5-cc71-4188-94a8-b06eff1d1889" ac:name="noformat" ac:schema-version="1"><plain-text-body> |
| gcj -shared -findirect-dispatch -fjni -fPIC -Wl,-symbolic -o MQAdapter.so MQAdapter.jar |
| export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/app |
| |
| </plain-text-body></structured-macro></li></ul> |
| |
| |
| <ul><li>Compile the C++ code |
| <structured-macro ac:macro-id="53b7d6ad-6df6-41ac-9966-78ee51ade665" ac:name="noformat" ac:schema-version="1"><plain-text-body> |
| g++ -c Bootstrap.cpp |
| </plain-text-body></structured-macro></li></ul> |
| |
| |
| <ul><li>Link Bootstrap with the Java code |
| <structured-macro ac:macro-id="0f038e48-ac55-469e-8193-0f587d4e46bc" ac:name="noformat" ac:schema-version="1"><plain-text-body> |
| gcj -o Bootstrap Bootstrap.o -L /opt/app -lgcj -lstdc++ activeio-1.1.jar.so activemq-core-3.2.jar.so |
| commons-logging-1.0.3.jar.so concurrent-1.3.4.jar.so geronimo-spec-jms-1.1-rc4.jar.so |
| geronimo-spec-j2ee-management-1.0-rc4.jar.so geronimo-spec-j2ee-jacc-1.0-rc4.jar.so |
| geronimo-spec-jta-1.0.1B-rc4.jar.so log4j-1.2.8.jar.so MQAdapter.so |
| </plain-text-body></structured-macro></li></ul> |
| |
| |
| <p>Now, if everything went ok you should be able to run the app. with ./Bootstrap.</p> |
| |
| <h4>References</h4> |
| |
| <p><a shape="rect" href="http://gcc.gnu.org/wiki/How%20to%20BC%20compile%20with%20GCJ">How to BC compile with GCJ</a></p> |
| |
| <p><a shape="rect" href="http://www.redhat.com/magazine/012oct05/features/java/">The state of Java on Linux</a></p></div> |
| |