Aggiunto l'esempio del capitolo sulle Java Message Service
This commit is contained in:
66
jmsdemo/pom.xml
Normal file
66
jmsdemo/pom.xml
Normal file
@@ -0,0 +1,66 @@
|
||||
<?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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>jmsdemo</artifactId>
|
||||
<name>jmsdemo</name>
|
||||
|
||||
<parent>
|
||||
<artifactId>jeedemo</artifactId>
|
||||
<groupId>it.plague.jeedemo</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.7</maven.compiler.source>
|
||||
<maven.compiler.target>1.7</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish.main.extras</groupId>
|
||||
<artifactId>glassfish-embedded-all</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.8.0</version>
|
||||
<configuration>
|
||||
<source>1.7</source>
|
||||
<target>1.7</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-failsafe-plugin</artifactId>
|
||||
<version>2.12.4</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>integration-test</id>
|
||||
<goals>
|
||||
<goal>integration-test</goal>
|
||||
<goal>verify</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,25 @@
|
||||
package it.plague.jeedemo;
|
||||
|
||||
import javax.ejb.ActivationConfigProperty;
|
||||
import javax.ejb.MessageDriven;
|
||||
import javax.jms.JMSException;
|
||||
import javax.jms.Message;
|
||||
import javax.jms.MessageListener;
|
||||
|
||||
@MessageDriven(mappedName = "jms/javaee7/Topic",
|
||||
activationConfig = {
|
||||
@ActivationConfigProperty(propertyName = "acknowledgeMod", propertyValue = "Auto-acknowledge"),
|
||||
@ActivationConfigProperty(propertyName = "messageSelector", propertyValue = "orderAmount > 1000")
|
||||
})
|
||||
public class ExpensiveOrderMDB implements MessageListener {
|
||||
|
||||
@Override
|
||||
public void onMessage(Message message) {
|
||||
try {
|
||||
OrderDTO order = message.getBody(OrderDTO.class);
|
||||
System.out.println("Expensive order received: " + order);
|
||||
} catch (JMSException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
29
jmsdemo/src/main/java/it/plague/jeedemo/OrderConsumer.java
Normal file
29
jmsdemo/src/main/java/it/plague/jeedemo/OrderConsumer.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package it.plague.jeedemo;
|
||||
|
||||
import javax.jms.ConnectionFactory;
|
||||
import javax.jms.Destination;
|
||||
import javax.jms.JMSContext;
|
||||
import javax.naming.Context;
|
||||
import javax.naming.InitialContext;
|
||||
import javax.naming.NamingException;
|
||||
|
||||
public class OrderConsumer {
|
||||
|
||||
public static void main(String[] args) throws NamingException {
|
||||
// Get the JNDI context
|
||||
Context jndiContext = new InitialContext();
|
||||
|
||||
// Looks up the administered objects
|
||||
ConnectionFactory connectionFactory =
|
||||
(ConnectionFactory) jndiContext.lookup("jms/javaee7/ConnectionFactory");
|
||||
Destination destination =
|
||||
(Destination) jndiContext.lookup("jms/javaee7/Topic");
|
||||
|
||||
try (JMSContext context = connectionFactory.createContext()) {
|
||||
while (true) {
|
||||
OrderDTO order = context.createConsumer(destination).receiveBody(OrderDTO.class);
|
||||
System.out.println("Order received: " + order);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
18
jmsdemo/src/main/java/it/plague/jeedemo/OrderDTO.java
Normal file
18
jmsdemo/src/main/java/it/plague/jeedemo/OrderDTO.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package it.plague.jeedemo;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class OrderDTO implements Serializable {
|
||||
|
||||
private Long orderId;
|
||||
private Date creationDate;
|
||||
private String customerName;
|
||||
private Float totalAmount;
|
||||
}
|
||||
32
jmsdemo/src/main/java/it/plague/jeedemo/OrderProducer.java
Normal file
32
jmsdemo/src/main/java/it/plague/jeedemo/OrderProducer.java
Normal file
@@ -0,0 +1,32 @@
|
||||
package it.plague.jeedemo;
|
||||
|
||||
import java.util.Date;
|
||||
import javax.jms.ConnectionFactory;
|
||||
import javax.jms.Destination;
|
||||
import javax.jms.JMSContext;
|
||||
import javax.naming.Context;
|
||||
import javax.naming.InitialContext;
|
||||
import javax.naming.NamingException;
|
||||
|
||||
public class OrderProducer {
|
||||
|
||||
public static void main(String[] args) throws NamingException {
|
||||
// Creates an orderDto with a total amount parameter
|
||||
Float totalAmount = Float.valueOf(args[0]);
|
||||
OrderDTO order = new OrderDTO(12342L, new Date(), "Betty Moreu", totalAmount);
|
||||
|
||||
// Get the JNDI context
|
||||
Context jndiContext = new InitialContext();
|
||||
|
||||
// Looks up the administered objects
|
||||
ConnectionFactory connectionFactory =
|
||||
(ConnectionFactory) jndiContext.lookup("jms/javaee7/ConnectionFactory");
|
||||
Destination destination =
|
||||
(Destination) jndiContext.lookup("jms/javaee7/Topic");
|
||||
|
||||
try (JMSContext context = connectionFactory.createContext()) {
|
||||
// Sends an object message to the topic
|
||||
context.createProducer().setProperty("orderAmount", totalAmount).send(destination, order);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user