diff --git a/spring-remoting/pom.xml b/spring-remoting/pom.xml index 52d670a726..96e808477f 100644 --- a/spring-remoting/pom.xml +++ b/spring-remoting/pom.xml @@ -36,6 +36,7 @@ remoting-http remoting-hessian-burlap + remoting-amqp \ No newline at end of file diff --git a/spring-remoting/remoting-amqp/pom.xml b/spring-remoting/remoting-amqp/pom.xml new file mode 100644 index 0000000000..6a7d0804dc --- /dev/null +++ b/spring-remoting/remoting-amqp/pom.xml @@ -0,0 +1,21 @@ + + + spring-remoting + com.baeldung + 1.0-SNAPSHOT + + 4.0.0 + + remoting-amqp + pom + + + + remoting-amqp + + remoting-amqp-server + remoting-amqp-client + + + diff --git a/spring-remoting/remoting-amqp/remoting-amqp-client/pom.xml b/spring-remoting/remoting-amqp/remoting-amqp-client/pom.xml new file mode 100644 index 0000000000..9a2d0bf9cc --- /dev/null +++ b/spring-remoting/remoting-amqp/remoting-amqp-client/pom.xml @@ -0,0 +1,36 @@ + + + remoting-amqp + com.baeldung + 1.0-SNAPSHOT + + 4.0.0 + + remoting-amqp-client + jar + + remoting-amqp-client + http://maven.apache.org + + + UTF-8 + + + + + org.springframework.boot + spring-boot-starter-amqp + + + org.springframework.boot + spring-boot-starter-tomcat + + + + + com.baeldung + api + + + diff --git a/spring-remoting/remoting-amqp/remoting-amqp-client/src/main/java/com/baeldung/client/AmqpClient.java b/spring-remoting/remoting-amqp/remoting-amqp-client/src/main/java/com/baeldung/client/AmqpClient.java new file mode 100644 index 0000000000..def80f6b5e --- /dev/null +++ b/spring-remoting/remoting-amqp/remoting-amqp-client/src/main/java/com/baeldung/client/AmqpClient.java @@ -0,0 +1,46 @@ +package com.baeldung.client; + +import com.baeldung.api.BookingException; +import com.baeldung.api.CabBookingService; +import org.springframework.amqp.core.*; +import org.springframework.amqp.rabbit.connection.ConnectionFactory; +import org.springframework.amqp.rabbit.core.RabbitTemplate; +import org.springframework.amqp.remoting.client.AmqpProxyFactoryBean; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; + +import static java.lang.System.out; + +@SpringBootApplication public class AmqpClient { + + @Bean Queue queue() { + return new Queue("remotingQueue"); + } + + @Bean AmqpProxyFactoryBean amqpFactoryBean(AmqpTemplate amqpTemplate) { + AmqpProxyFactoryBean factoryBean = new AmqpProxyFactoryBean(); + factoryBean.setServiceInterface(CabBookingService.class); + factoryBean.setAmqpTemplate(amqpTemplate); + return factoryBean; + } + + @Bean Exchange directExchange(Queue someQueue) { + DirectExchange exchange = new DirectExchange("remoting.exchange"); + BindingBuilder.bind(someQueue).to(exchange).with("remoting.binding"); + return exchange; + } + + @Bean RabbitTemplate amqpTemplate(ConnectionFactory factory) { + RabbitTemplate template = new RabbitTemplate(factory); + template.setRoutingKey("remoting.binding"); + template.setExchange("remoting.exchange"); + return template; + } + + public static void main(String[] args) throws BookingException { + CabBookingService service = SpringApplication.run(AmqpClient.class, args).getBean(CabBookingService.class); + out.println(service.bookRide("13 Seagate Blvd, Key Largo, FL 33037")); + } + +} diff --git a/spring-remoting/remoting-amqp/remoting-amqp-client/src/main/resources/application.properties b/spring-remoting/remoting-amqp/remoting-amqp-client/src/main/resources/application.properties new file mode 100644 index 0000000000..8df44a7c8d --- /dev/null +++ b/spring-remoting/remoting-amqp/remoting-amqp-client/src/main/resources/application.properties @@ -0,0 +1,19 @@ +# This is true to make SpringBoot to automatically register a bean of type 'org.springframework.amqp.core.AmqpAdmin'. +# Check the org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration javadoc for details. +spring.rabbitmq.dynamic=true + +# The port to which the client should connect defaults to 5672. +spring.rabbitmq.port=32769 + +# Username and password +spring.rabbitmq.username=guest +spring.rabbitmq.password=guest + +# The host, defaults to localhost. +spring.rabbitmq.host=192.168.99.100 + +# Logging +logging.pattern.console=%d{mm:ss.SSS} %-5p [%-31t] [%-54logger{0}] %marker%m%ex{full} - %logger - %F:%L%n +logging.level.root=WARN +logging.level.org.springframework.amqp=TRACE + diff --git a/spring-remoting/remoting-amqp/remoting-amqp-server/pom.xml b/spring-remoting/remoting-amqp/remoting-amqp-server/pom.xml new file mode 100644 index 0000000000..4d3f4da765 --- /dev/null +++ b/spring-remoting/remoting-amqp/remoting-amqp-server/pom.xml @@ -0,0 +1,46 @@ + + + remoting-amqp + com.baeldung + 1.0-SNAPSHOT + + 4.0.0 + + remoting-amqp-server + jar + + remoting-amqp-server + http://maven.apache.org + + + UTF-8 + + + + + com.baeldung + api + ${project.version} + + + org.springframework.boot + spring-boot-starter-amqp + + + com.baeldung + api + 1.0-SNAPSHOT + + + junit + junit + test + + + com.baeldung + spring-remoting-http-server + 1.0-SNAPSHOT + + + diff --git a/spring-remoting/remoting-amqp/remoting-amqp-server/src/main/java/com/baeldung/server/AmqpServer.java b/spring-remoting/remoting-amqp/remoting-amqp-server/src/main/java/com/baeldung/server/AmqpServer.java new file mode 100644 index 0000000000..f0155b2141 --- /dev/null +++ b/spring-remoting/remoting-amqp/remoting-amqp-server/src/main/java/com/baeldung/server/AmqpServer.java @@ -0,0 +1,53 @@ +package com.baeldung.server; + +import com.baeldung.api.CabBookingService; +import org.springframework.amqp.core.AmqpTemplate; +import org.springframework.amqp.core.Queue; +import org.springframework.amqp.rabbit.connection.ConnectionFactory; +import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer; +import org.springframework.amqp.remoting.service.AmqpInvokerServiceExporter; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +@Configuration @ComponentScan @EnableAutoConfiguration +public class AmqpServer { + + /* + Please note that + - CachingConnectionFactory + - RabbitAdmin + - AmqpTemplate + are automatically declared by SpringBoot. + */ + + @Bean CabBookingService bookingService() { + return new CabBookingServiceImpl(); + } + + @Bean Queue queue() { + return new Queue("remotingQueue"); + } + + @Bean AmqpInvokerServiceExporter exporter(CabBookingService implementation, AmqpTemplate template) { + AmqpInvokerServiceExporter exporter = new AmqpInvokerServiceExporter(); + exporter.setServiceInterface(CabBookingService.class); + exporter.setService(implementation); + exporter.setAmqpTemplate(template); + return exporter; + } + + @Bean SimpleMessageListenerContainer listener(ConnectionFactory factory, AmqpInvokerServiceExporter exporter, Queue queue) { + SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(factory); + container.setMessageListener(exporter); + container.setQueueNames(queue.getName()); + return container; + } + + public static void main(String[] args) { + SpringApplication.run(AmqpServer.class, args); + } + +} \ No newline at end of file diff --git a/spring-remoting/remoting-amqp/remoting-amqp-server/src/main/resources/application.properties b/spring-remoting/remoting-amqp/remoting-amqp-server/src/main/resources/application.properties new file mode 100644 index 0000000000..8df44a7c8d --- /dev/null +++ b/spring-remoting/remoting-amqp/remoting-amqp-server/src/main/resources/application.properties @@ -0,0 +1,19 @@ +# This is true to make SpringBoot to automatically register a bean of type 'org.springframework.amqp.core.AmqpAdmin'. +# Check the org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration javadoc for details. +spring.rabbitmq.dynamic=true + +# The port to which the client should connect defaults to 5672. +spring.rabbitmq.port=32769 + +# Username and password +spring.rabbitmq.username=guest +spring.rabbitmq.password=guest + +# The host, defaults to localhost. +spring.rabbitmq.host=192.168.99.100 + +# Logging +logging.pattern.console=%d{mm:ss.SSS} %-5p [%-31t] [%-54logger{0}] %marker%m%ex{full} - %logger - %F:%L%n +logging.level.root=WARN +logging.level.org.springframework.amqp=TRACE +