{"id":6047,"date":"2016-12-14T10:19:37","date_gmt":"2016-12-14T15:19:37","guid":{"rendered":"http:\/\/appcrawler.com\/wordpress\/?p=6047"},"modified":"2016-12-14T11:20:22","modified_gmt":"2016-12-14T16:20:22","slug":"using-spring-amqp-to-post-messages-to-rabbitmq","status":"publish","type":"post","link":"http:\/\/appcrawler.com\/wordpress\/2016\/12\/14\/using-spring-amqp-to-post-messages-to-rabbitmq\/","title":{"rendered":"Using Spring AMQP to post messages to RabbitMQ"},"content":{"rendered":"<p>The ActiveMQ jar&#8217;s come with the AMQP 1.0 protocol, which doesn&#8217;t work against a RabbitMQ AMQP version 0.9.1 broker.  As such, we use a spring jar to get this to work, thanks to <a href=https:\/\/dzone.com\/articles\/mocking-rabbitmq-for-integration-tests target=_blank>this link<\/a>.<\/p>\n<p>Very simple example of this.<\/p>\n<p>Maven pom.xml file&#8230;<\/p>\n<pre>\r\n<project xmlns=\"http:\/\/maven.apache.org\/POM\/4.0.0\" xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\r\n  xsi:schemaLocation=\"http:\/\/maven.apache.org\/POM\/4.0.0 http:\/\/maven.apache.org\/maven-v4_0_0.xsd\">\r\n  <modelVersion>4.0.0<\/modelVersion>\r\n  <groupId>com.express.it.arch.poc.amqp<\/groupId>\r\n  <artifactId>amqppoc<\/artifactId>\r\n  <packaging>jar<\/packaging>\r\n  <version>1.0-SNAPSHOT<\/version>\r\n  <name>amqppoc<\/name>\r\n  <url>http:\/\/maven.apache.org<\/url>\r\n  <dependencies>\r\n    <dependency>\r\n      <groupId>junit<\/groupId>\r\n      <artifactId>junit<\/artifactId>\r\n      <version>3.8.1<\/version>\r\n      <scope>test<\/scope>\r\n    <\/dependency>\r\n    <dependency>\r\n      <groupId>org.springframework.amqp<\/groupId>\r\n      <artifactId>spring-rabbit<\/artifactId>\r\n      <version>1.5.2.RELEASE<\/version>\r\n    <\/dependency>\r\n  <\/dependencies>\r\n\r\n  <build>\r\n    <pluginManagement>\r\n      <plugins>\r\n        <plugin>\r\n          <groupId>org.apache.maven.plugins<\/groupId>\r\n          <artifactId>maven-assembly-plugin<\/artifactId>\r\n          <configuration>\r\n            <archive>\r\n              <manifest><mainClass>com.yourcompany.it.arch.poc.amqp.App<\/mainClass><\/manifest>\r\n            <\/archive>\r\n            <descriptorRefs>\r\n              <descriptorRef>jar-with-dependencies<\/descriptorRef>\r\n            <\/descriptorRefs>\r\n          <\/configuration>\r\n        <\/plugin>\r\n      <\/plugins>\r\n    <\/pluginManagement>\r\n  <\/build>\r\n<\/project>\r\n<\/pre>\n<p>Java class for test&#8230;<\/p>\n<pre>\r\npackage com.express.it.arch.poc.amqp;\r\n\r\nimport org.springframework.amqp.core.*;\r\nimport org.springframework.amqp.rabbit.core.*;\r\nimport org.springframework.amqp.rabbit.connection.CachingConnectionFactory;\r\nimport org.springframework.amqp.rabbit.core.RabbitAdmin;\r\n\r\npublic class App {\r\n  public static void main (String args[]) throws Exception {\r\n\tApp a = new App();\r\n    a.createExchange(\"foo\");\r\n    a.sendMessage(\"i came, i saw, i conquered\");\r\n    Thread.sleep(5000);\r\n    a.receiveMessage();\r\n  }\r\n  private void createExchange(final String identifier) {\r\n    final CachingConnectionFactory cf = new CachingConnectionFactory(5672);\r\n    cf.setUsername(\"admin\");\r\n    cf.setPassword(\"admin\");\r\n    final RabbitAdmin admin = new RabbitAdmin(cf);\r\n    final Queue queue = new Queue(\"myQueue\", false);\r\n    admin.declareQueue(queue);\r\n    final TopicExchange exchange = new TopicExchange(\"myExchange\");\r\n    admin.declareExchange(exchange);\r\n    admin.declareBinding(BindingBuilder.bind(queue).to(exchange).with(\"#\"));\r\n    cf.destroy();\r\n  }\r\n\r\n  private void deleteExchange() {\r\n    final CachingConnectionFactory cf = new CachingConnectionFactory(5672);\r\n    final RabbitAdmin admin = new RabbitAdmin(cf);\r\n    admin.deleteExchange(\"myExchange\");\r\n    cf.destroy();\r\n  }\r\n\r\n  public void sendMessage(final String message) throws Exception {\r\n    final CachingConnectionFactory cf = new CachingConnectionFactory(5672);\r\n    cf.setUsername(\"admin\");\r\n    cf.setPassword(\"admin\");\r\n    final RabbitTemplate template = new RabbitTemplate(cf);\r\n    template.convertAndSend(\"myExchange\", \"#\", message);\r\n    cf.destroy();\r\n  }\r\n  public void receiveMessage() throws Exception {\r\n    final CachingConnectionFactory cf = new CachingConnectionFactory(5672);\r\n    cf.setUsername(\"admin\");\r\n    cf.setPassword(\"admin\");\r\n    final RabbitTemplate template = new RabbitTemplate(cf);\r\n    String s = (String)template.receiveAndConvert(\"myQueue\");\r\n    System.out.println(\"Message received = \" + s);\r\n    cf.destroy();\r\n  }\r\n}\r\n<\/pre>\n<p>Run build&#8230;<\/p>\n<pre>\r\nmvn clean compile assembly:single\r\n<\/pre>\n<p>Run test&#8230;<\/p>\n<pre>\r\njava -cp target\\amqppoc-1.0-SNAPSHOT-jar-with-dependencies.jar com.express.it.arch.poc.amqp.App\r\n<\/pre>\n<p>You should see the message on the command line after five seconds.  If you can GET the message in the RabbitMQ UI, available at http:\/\/localhost:15672\/#\/queues\/myQueue, before the five seconds elapses, you should see the following&#8230;<\/p>\n<p><img alt='' class='alignnone size-full wp-image-6051 ' src='http:\/\/appcrawler.com\/wordpress\/wp-content\/uploads\/2016\/12\/img_58516297204d2.png' \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The ActiveMQ jar&#8217;s come with the AMQP 1.0 protocol, which doesn&#8217;t work against a RabbitMQ AMQP version 0.9.1 broker. As such, we use a spring jar to get this to work, thanks to this link. Very simple example of this.&hellip;<\/p>\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"http:\/\/appcrawler.com\/wordpress\/2016\/12\/14\/using-spring-amqp-to-post-messages-to-rabbitmq\/\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"footnotes":""},"categories":[25,62,63],"tags":[],"_links":{"self":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/6047"}],"collection":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/comments?post=6047"}],"version-history":[{"count":9,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/6047\/revisions"}],"predecessor-version":[{"id":6058,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/6047\/revisions\/6058"}],"wp:attachment":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/media?parent=6047"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/categories?post=6047"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/tags?post=6047"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}