Friday, June 12, 2015

Call RabbitMQ in Matlab

--------------------------- ###Installation Environment ``` Ubuntu 12.04, 64-bit Matlab R2012a, 64-bit ``` --------------------------- ###Install Package Install open JDK and check the version ```bash sudo apt-get install openjdk-6-jdk sudo apt-get install openjdk-6-jre javac -version java -version ``` --------------------------- ###Code of Send.java and Recv.java Send.java ```java import com.rabbitmq.client.ConnectionFactory; import com.rabbitmq.client.Connection; import com.rabbitmq.client.Channel; public class Send { private final static String QUEUE_NAME = "hello"; public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, false, false, false, null); String message = "Hello World!"; channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8")); System.out.println(" [x] Sent '" + message + "'"); channel.close(); connection.close(); } } ``` Recv.java ```java import com.rabbitmq.client.ConnectionFactory; import com.rabbitmq.client.Connection; import com.rabbitmq.client.Channel; import com.rabbitmq.client.QueueingConsumer; public class Recv { private final static String QUEUE_NAME = "hello"; public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, false, false, false, null); System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(QUEUE_NAME, true, consumer); while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody(),"UTF-8"); System.out.println(" [x] Received '" + message + "'"); } } } ``` --------------------------- ###Compile and run Send.java and Recv.java ```bash wget rabbitmq-java-client-bin-3.5.3.zip unzip rabbitmq-java-client-bin-3.5.3.zip cp rabbitmq-java-client-bin-3.5.3/*.jar ./ javac -cp rabbitmq-client.jar Send.java Recv.java # This will produce Send.class and Recv.class ``` In one terminal ```bash java -cp .:commons-io-1.2.jar:commons-cli-1.1.jar:rabbitmq-client.jar Send [x] Sent 'Hello World!' java -cp .:commons-io-1.2.jar:commons-cli-1.1.jar:rabbitmq-client.jar Send [x] Sent 'Hello World!' ``` In another terminal ```bash java -cp .:commons-io-1.2.jar:commons-cli-1.1.jar:rabbitmq-client.jar Recv [*] Waiting for messages. To exit press CTRL+C [x] Received 'Hello World!' [x] Received 'Hello World!' ``` --------------------------- ### Run in Matlab ```matlab >> javaaddpath('/path/to/Send.class','/path/to/Recv.class', ... '/path/to/commons-io-1.2.jar', ... '/path/to/commons-cli-1.1.jar', '/path/to/rabbitmq-client.jar'); >> clientSend = Send; >> javaMethod('main', clientSend, '') [x] Sent 'Hello World!' >> javaMethod('main', clientSend, '') [x] Sent 'Hello World!' >> serverRecv = Recv; >> javaMethod('main', serverRecv, '') [x] Received 'Hello World!' [x] Received 'Hello World!' ``` --------------------------- ###Reference https://github.com/rabbitmq/rabbitmq-tutorials/tree/master/java

Thursday, June 11, 2015

Three Ways to Call A Simple "Hello World" Java Program in MATLAB

--------------------------- ###Installation Environment Environment ``` Ubuntu 12.04, 64-bit Matlab R2012a, 64-bit ``` --------------------------- ###Install Package Install open JDK and check the version ```bash $ sudo apt-get install openjdk-6-jdk $ sudo apt-get install openjdk-6-jre $ javac -version $ java -version ``` A simple HelloWorld.java code ```java public class HelloWorld { public static void main( String args[] ) { System.out.println( "Hello World!" ); } } ``` --------------------------- ### The First Way Compile and run HelloWorld ```bash $ javac -cp %CLASSPATH:/path/to/matlab/jre HelloWorld.java $ jar -cvf library.jar HelloWorld.class ``` Example1.m ```matlab >> clear all; >> javaaddpath(/path/to/library.jar); >> HelloWorld ``` --------------------------- ### The Second Way Compile and run HelloWorld ```bash $ javac HelloWorld.java # This will produce a HelloWorld.class file $ java HelloWorld ``` Example2.m ```matlab >> java -version % consistent with the java version installed in system >> which classpath.txt % add the path HelloWorld.class to the end of classpath.txt >> javaaddpath(/path/to/matlab/HelloWorld.class); >> o = HelloWorld; >> javaMethod('main', o); ``` --------------------------- ### The Third Way Compile and run HelloWorld ```bash $ javac HelloWorld.java # This will produce a HelloWorld.class file $ java HelloWorld ``` Example3.m ```matlab >> java -version % consistent with the java version installed in system >> which classpath.txt % add the path HelloWorld.class to the end of classpath.txt /usr/local/MATLAB/R2012a/toolbox/local/classpath.txt >> o = HelloWorld; >> javaMethod('main', o, ''); ``` --------------------------- Reference: http://www.mathworks.com/matlabcentral/answers/99993-how-do-i-call-a-simple-hello-world-java-program-in-matlab https://sergiolima.wordpress.com/2009/10/15/using-java-objects-with-matlab/