Bael 556 - spring remoting with jms (#1900)

* Burlap & Hessian server added

* Burlap & Hessian client work

* Fixed main

* Fixed formatting

* Spring Remote example based on Burlap & Hessian runs in a JUnit test

* Fixed main

* Fixed formatting

* Spring Remote example based on Burlap & Hessian runs in a JUnit test

* Spring Remote example based on Burlap & Hessian runs in a JUnit test

* Burlap & Hessian client work

* Fixed main

* Fixed main

* Fixed formatting

* Fixed formatting

* Spring Remote example based on Burlap & Hessian runs in a JUnit test

* Spring Remote example based on Burlap & Hessian runs in a JUnit test

* Fixed POM

* first step, compile

* works!

* works with proper list of trusted packages

* works the SpringBoot style

* Fixed indentation.
This commit is contained in:
Daniele Demichelis 2017-05-23 08:47:18 +02:00 committed by Grzegorz Piwowarek
parent 3b1337598f
commit 4ded9c02f7
9 changed files with 221 additions and 0 deletions

View File

@ -32,6 +32,7 @@
<module>remoting-http</module>
<module>remoting-hessian-burlap</module>
<module>remoting-amqp</module>
<module>remoting-jms</module>
</modules>
</project>

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-remoting</artifactId>
<groupId>com.baeldung</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<modules>
<module>remoting-jms-client</module>
<module>remoting-jms-server</module>
</modules>
<artifactId>remoting-jms</artifactId>
</project>

View File

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>remoting-jms</artifactId>
<groupId>com.baeldung</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>remoting-jms-client</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.baeldung</groupId>
<artifactId>api</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -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);
}
}

View File

@ -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

View File

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>remoting-jms</artifactId>
<groupId>com.baeldung</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>remoting-jms-server</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.baeldung</groupId>
<artifactId>api</artifactId>
</dependency>
<dependency>
<groupId>com.baeldung</groupId>
<artifactId>api</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>

View File

@ -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());
}
}

View File

@ -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);
}
}

View File

@ -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