Aggiunto l'esempio del capitolo sulle Java Message Service

This commit is contained in:
Fabio Scotto di Santolo
2019-04-02 12:31:18 +02:00
parent e31da8e118
commit 9e07e624e0
9 changed files with 176 additions and 29 deletions

3
.idea/compiler.xml generated
View File

@@ -2,6 +2,7 @@
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile default="true" name="Default" enabled="true" />
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
@@ -9,7 +10,9 @@
<module name="cdidemo" />
<module name="jpademo" />
<module name="beanvalidationdemo" />
<module name="jmsdemo" />
<module name="xmlandjsondemo" />
<module name="ejbdemo" />
</profile>
</annotationProcessing>
<bytecodeTargetLevel>

2
.idea/encodings.xml generated
View File

@@ -3,6 +3,8 @@
<component name="Encoding">
<file url="file://$PROJECT_DIR$/beanvalidationdemo" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/cdidemo" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/ejbdemo" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/jmsdemo" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/jpademo" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/xmlandjsondemo" charset="UTF-8" />
</component>

View File

@@ -12,33 +12,4 @@
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/target/generated-sources/annotations" isTestSource="false" generated="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: org.eclipse.persistence:org.eclipse.persistence.jpa:2.5.0" level="project" />
<orderEntry type="library" name="Maven: org.eclipse.persistence:javax.persistence:2.1.0" level="project" />
<orderEntry type="library" name="Maven: org.eclipse.persistence:org.eclipse.persistence.asm:2.5.0" level="project" />
<orderEntry type="library" name="Maven: org.eclipse.persistence:org.eclipse.persistence.antlr:2.5.0" level="project" />
<orderEntry type="library" name="Maven: org.eclipse.persistence:org.eclipse.persistence.jpa.jpql:2.5.0" level="project" />
<orderEntry type="library" name="Maven: org.eclipse.persistence:org.eclipse.persistence.core:2.5.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.eclipse.persistence:org.eclipse.persistence.jpa.modelgen.processor:2.5.0" level="project" />
<orderEntry type="library" name="Maven: org.hibernate.validator:hibernate-validator:6.0.16.Final" level="project" />
<orderEntry type="library" name="Maven: javax.validation:validation-api:2.0.1.Final" level="project" />
<orderEntry type="library" name="Maven: org.jboss.logging:jboss-logging:3.3.2.Final" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml:classmate:1.3.4" level="project" />
<orderEntry type="library" name="Maven: org.glassfish:javax.el:3.0.1-b09" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.projectlombok:lombok:1.18.6" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.glassfish.main.extras:glassfish-embedded-all:4.1.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
</component>
</module>

66
jmsdemo/pom.xml Normal file
View 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>

View File

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

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

View 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;
}

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

View File

@@ -14,6 +14,7 @@
<module>jpademo</module>
<module>ejbdemo</module>
<module>xmlandjsondemo</module>
<module>jmsdemo</module>
</modules>
<dependencyManagement>