diff --git a/spring-remoting/pom.xml b/spring-remoting/pom.xml index 8acb38bd44..0b751d1fc9 100644 --- a/spring-remoting/pom.xml +++ b/spring-remoting/pom.xml @@ -32,6 +32,7 @@ remoting-http remoting-hessian-burlap remoting-amqp + remoting-jms \ No newline at end of file diff --git a/spring-remoting/remoting-jms/pom.xml b/spring-remoting/remoting-jms/pom.xml new file mode 100644 index 0000000000..fe36431423 --- /dev/null +++ b/spring-remoting/remoting-jms/pom.xml @@ -0,0 +1,19 @@ + + + + spring-remoting + com.baeldung + 1.0-SNAPSHOT + + 4.0.0 + pom + + remoting-jms-client + remoting-jms-server + + remoting-jms + + + \ No newline at end of file diff --git a/spring-remoting/remoting-jms/remoting-jms-client/pom.xml b/spring-remoting/remoting-jms/remoting-jms-client/pom.xml new file mode 100644 index 0000000000..01b7bea657 --- /dev/null +++ b/spring-remoting/remoting-jms/remoting-jms-client/pom.xml @@ -0,0 +1,35 @@ + + + + remoting-jms + com.baeldung + 1.0-SNAPSHOT + + 4.0.0 + + remoting-jms-client + + + UTF-8 + + + + + org.springframework.boot + spring-boot-starter-activemq + + + org.springframework.boot + spring-boot-starter-tomcat + + + + + com.baeldung + api + + + + \ No newline at end of file diff --git a/spring-remoting/remoting-jms/remoting-jms-client/src/main/java/com/baeldung/client/JmsClient.java b/spring-remoting/remoting-jms/remoting-jms-client/src/main/java/com/baeldung/client/JmsClient.java new file mode 100644 index 0000000000..0c5d728f07 --- /dev/null +++ b/spring-remoting/remoting-jms/remoting-jms-client/src/main/java/com/baeldung/client/JmsClient.java @@ -0,0 +1,39 @@ +package com.baeldung.client; + +import com.baeldung.api.Booking; +import com.baeldung.api.BookingException; +import com.baeldung.api.CabBookingService; +import org.apache.activemq.command.ActiveMQQueue; +import org.springframework.beans.factory.FactoryBean; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.jms.remoting.JmsInvokerProxyFactoryBean; + +import javax.jms.ConnectionFactory; +import javax.jms.Queue; + +@SpringBootApplication +public class JmsClient { + + @Bean Queue queue() { + return new ActiveMQQueue("remotingQueue"); +} + + @Bean FactoryBean invoker(ConnectionFactory factory, Queue queue) { + JmsInvokerProxyFactoryBean factoryBean = new JmsInvokerProxyFactoryBean(); + factoryBean.setConnectionFactory(factory); + factoryBean.setServiceInterface(CabBookingService.class); + factoryBean.setQueue(queue); + return factoryBean; + } + + public static void main(String[] args) throws BookingException { + CabBookingService service = SpringApplication.run(JmsClient.class, args).getBean(CabBookingService.class); + System.out.println("here"); + Booking bookingOutcome = service.bookRide("13 Seagate Blvd, Key Largo, FL 33037"); + System.out.println("there"); + System.out.println(bookingOutcome); + } + +} diff --git a/spring-remoting/remoting-jms/remoting-jms-client/src/main/resources/application.properties b/spring-remoting/remoting-jms/remoting-jms-client/src/main/resources/application.properties new file mode 100644 index 0000000000..738b030a59 --- /dev/null +++ b/spring-remoting/remoting-jms/remoting-jms-client/src/main/resources/application.properties @@ -0,0 +1,10 @@ +spring.activemq.broker-url=tcp://127.0.0.1:61616 +spring.activemq.packages.trusted=org.springframework.remoting.support,java.lang,com.baeldung.api + +# 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.apache.activemq=DEBUG +logging.level.org.springframework.jms=DEBUG +logging.level.org.springframework=WARN + diff --git a/spring-remoting/remoting-jms/remoting-jms-server/pom.xml b/spring-remoting/remoting-jms/remoting-jms-server/pom.xml new file mode 100644 index 0000000000..8feac4df57 --- /dev/null +++ b/spring-remoting/remoting-jms/remoting-jms-server/pom.xml @@ -0,0 +1,40 @@ + + + + remoting-jms + com.baeldung + 1.0-SNAPSHOT + + 4.0.0 + + remoting-jms-server + + + UTF-8 + + + + + org.springframework.boot + spring-boot-starter-activemq + + + org.springframework.boot + spring-boot-starter-tomcat + + + + + com.baeldung + api + + + com.baeldung + api + ${project.version} + + + + \ No newline at end of file diff --git a/spring-remoting/remoting-jms/remoting-jms-server/src/main/java/com/baeldung/server/CabBookingServiceImpl.java b/spring-remoting/remoting-jms/remoting-jms-server/src/main/java/com/baeldung/server/CabBookingServiceImpl.java new file mode 100644 index 0000000000..55ec9c5733 --- /dev/null +++ b/spring-remoting/remoting-jms/remoting-jms-server/src/main/java/com/baeldung/server/CabBookingServiceImpl.java @@ -0,0 +1,16 @@ +package com.baeldung.server; + +import com.baeldung.api.Booking; +import com.baeldung.api.BookingException; +import com.baeldung.api.CabBookingService; + +import static java.lang.Math.random; +import static java.util.UUID.randomUUID; + +public class CabBookingServiceImpl implements CabBookingService { + + @Override public Booking bookRide(String pickUpLocation) throws BookingException { + if (random() < 0.3) throw new BookingException("Cab unavailable"); + return new Booking(randomUUID().toString()); + } +} diff --git a/spring-remoting/remoting-jms/remoting-jms-server/src/main/java/com/baeldung/server/JmsServer.java b/spring-remoting/remoting-jms/remoting-jms-server/src/main/java/com/baeldung/server/JmsServer.java new file mode 100644 index 0000000000..8572718042 --- /dev/null +++ b/spring-remoting/remoting-jms/remoting-jms-server/src/main/java/com/baeldung/server/JmsServer.java @@ -0,0 +1,51 @@ +package com.baeldung.server; + +import com.baeldung.api.CabBookingService; +import org.apache.activemq.command.ActiveMQQueue; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.jms.listener.SimpleMessageListenerContainer; +import org.springframework.jms.remoting.JmsInvokerServiceExporter; + +import javax.jms.ConnectionFactory; +import javax.jms.Queue; + +@SpringBootApplication public class JmsServer { + + /* + This server needs to be connected to an ActiveMQ server. + To quickly spin up an ActiveMQ server, you can use Docker. + + docker run -p 61616:61616 -p 8161:8161 rmohr/activemq:5.14.3 + */ + + @Bean CabBookingService bookingService() { + return new CabBookingServiceImpl(); + } + + @Bean Queue queue() { + return new ActiveMQQueue("remotingQueue"); +} + + @Bean JmsInvokerServiceExporter exporter(CabBookingService implementation) { + JmsInvokerServiceExporter exporter = new JmsInvokerServiceExporter(); + exporter.setServiceInterface(CabBookingService.class); + exporter.setService(implementation); + return exporter; + } + + @Bean SimpleMessageListenerContainer listener(ConnectionFactory factory, JmsInvokerServiceExporter exporter) { + SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); + container.setConnectionFactory(factory); + container.setDestinationName("remotingQueue"); + container.setConcurrentConsumers(1); + container.setMessageListener(exporter); + return container; + } + + public static void main(String[] args) { + SpringApplication.run(JmsServer.class, args); + } + +} diff --git a/spring-remoting/remoting-jms/remoting-jms-server/src/main/resources/application.properties b/spring-remoting/remoting-jms/remoting-jms-server/src/main/resources/application.properties new file mode 100644 index 0000000000..738b030a59 --- /dev/null +++ b/spring-remoting/remoting-jms/remoting-jms-server/src/main/resources/application.properties @@ -0,0 +1,10 @@ +spring.activemq.broker-url=tcp://127.0.0.1:61616 +spring.activemq.packages.trusted=org.springframework.remoting.support,java.lang,com.baeldung.api + +# 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.apache.activemq=DEBUG +logging.level.org.springframework.jms=DEBUG +logging.level.org.springframework=WARN +