esercizio e test usando jms e mdb
This commit is contained in:
@@ -9,8 +9,8 @@ import javax.inject.Inject;
|
||||
* Created by Giovanni on 24/05/2015.
|
||||
*/
|
||||
|
||||
@Singleton
|
||||
@Startup
|
||||
//@Singleton enable
|
||||
//@Startup enable
|
||||
public class InitializeDB {
|
||||
|
||||
private Student s1,s2;
|
||||
|
||||
71
exercise2/Book.java
Normal file
71
exercise2/Book.java
Normal file
@@ -0,0 +1,71 @@
|
||||
package exercise2;
|
||||
|
||||
import javax.persistence.*;
|
||||
import javax.validation.constraints.DecimalMin;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 31/05/2015.
|
||||
*/
|
||||
|
||||
@Entity
|
||||
@NamedQueries({
|
||||
@NamedQuery(name = "findbyisbn", query = "SELECT b FROM Book b WHERE b.isbn =?1"),
|
||||
@NamedQuery(name = "findall", query = "SELECT b FROM Book b")
|
||||
})
|
||||
public class Book implements Serializable{
|
||||
|
||||
@GeneratedValue @Id
|
||||
private Long id ;
|
||||
@NotNull
|
||||
private String title;
|
||||
@NotNull
|
||||
private String author;
|
||||
@NotNull
|
||||
private Float price;
|
||||
@NotNull
|
||||
private String isbn;
|
||||
|
||||
|
||||
public Book(){}
|
||||
|
||||
public Book(@NotNull String in_isbn,@NotNull String in_title,@NotNull String in_author, @NotNull @DecimalMin("0.0") Float in_price){
|
||||
title = in_title;
|
||||
author = in_author;
|
||||
price = in_price;
|
||||
isbn = in_isbn;
|
||||
}
|
||||
|
||||
public @NotNull String getIsbn() {
|
||||
return isbn;
|
||||
}
|
||||
|
||||
public void setIsbn(@NotNull String isbn) {
|
||||
this.isbn = isbn;
|
||||
}
|
||||
|
||||
public @NotNull String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(@NotNull String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public @NotNull String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setAuthor( @NotNull String author) {
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
public @NotNull @DecimalMin("0.0") Float getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(@NotNull @DecimalMin("0.0") Float price) {
|
||||
this.price = price;
|
||||
}
|
||||
}
|
||||
26
exercise2/BookMessage.java
Normal file
26
exercise2/BookMessage.java
Normal file
@@ -0,0 +1,26 @@
|
||||
package exercise2;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 31/05/2015.
|
||||
*/
|
||||
public class BookMessage implements Serializable{
|
||||
|
||||
public BookMessage (String in_isbn, Float in_price){
|
||||
isbn = in_isbn;
|
||||
price = in_price;
|
||||
}
|
||||
|
||||
|
||||
private String isbn;
|
||||
private Float price;
|
||||
|
||||
public String getIsbn() {
|
||||
return isbn;
|
||||
}
|
||||
|
||||
public Float getPrice() {
|
||||
return price;
|
||||
}
|
||||
}
|
||||
49
exercise2/EJBBook.java
Normal file
49
exercise2/EJBBook.java
Normal file
@@ -0,0 +1,49 @@
|
||||
package exercise2;
|
||||
|
||||
|
||||
|
||||
import javax.ejb.LocalBean;
|
||||
import javax.ejb.Stateless;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import javax.persistence.Query;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 24/05/2015.
|
||||
*/
|
||||
@Stateless
|
||||
@LocalBean
|
||||
public class EJBBook implements RemoteInterface {
|
||||
|
||||
@PersistenceContext(unitName = "exercise2")
|
||||
EntityManager em ;
|
||||
|
||||
|
||||
public void create_book (@NotNull Book s) {
|
||||
em.persist(s);
|
||||
}
|
||||
public void update_book (@NotNull Book s) {
|
||||
em.merge(s);
|
||||
em.persist(s);
|
||||
}
|
||||
public void delete_book (@NotNull Book s) {
|
||||
em.merge(s);
|
||||
em.remove(s);
|
||||
}
|
||||
|
||||
public @NotNull List<Book> findbyisbn (@NotNull String in_isbn) {
|
||||
Query Q = em.createNamedQuery("findbyisbn");
|
||||
Q.setParameter(1, in_isbn);
|
||||
return (List<Book>)Q.getResultList();
|
||||
}
|
||||
|
||||
|
||||
public List<Book> findall () {
|
||||
Query Q = em.createNamedQuery("findall");
|
||||
return (List<Book>)Q.getResultList();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
43
exercise2/InitializeDB.java
Normal file
43
exercise2/InitializeDB.java
Normal file
@@ -0,0 +1,43 @@
|
||||
package exercise2;
|
||||
|
||||
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.PreDestroy;
|
||||
import javax.ejb.Singleton;
|
||||
import javax.ejb.Startup;
|
||||
import javax.inject.Inject;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 24/05/2015.
|
||||
*/
|
||||
|
||||
@Singleton
|
||||
@Startup
|
||||
public class InitializeDB {
|
||||
|
||||
private Book b1,b2;
|
||||
|
||||
@Inject
|
||||
private EJBBook e;
|
||||
|
||||
@PostConstruct
|
||||
void pupulate (){
|
||||
b1 = new Book("0001","Titolo1","Author1",10f);
|
||||
b2 = new Book("0010","Titolo2","Author2",30f);
|
||||
e.create_book(b1);
|
||||
e.create_book(b2);
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
void delete () {
|
||||
e.delete_book(b1);
|
||||
e.delete_book(b2);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
40
exercise2/MDBBook.java
Normal file
40
exercise2/MDBBook.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package exercise2;
|
||||
|
||||
import javax.ejb.MessageDriven;
|
||||
import javax.inject.Inject;
|
||||
import javax.jms.JMSException;
|
||||
import javax.jms.Message;
|
||||
import javax.jms.MessageListener;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 31/05/2015.
|
||||
*/
|
||||
@MessageDriven(mappedName = "jms/javaee7/Topic")
|
||||
public class MDBBook implements MessageListener {
|
||||
|
||||
@Inject
|
||||
Logger l;
|
||||
|
||||
@Inject
|
||||
EJBBook e;
|
||||
|
||||
public void onMessage(Message message) {
|
||||
try {
|
||||
BookMessage m = message.getBody(BookMessage.class);
|
||||
l.info("Messaggio: " + m.getIsbn() + " " + m.getPrice());
|
||||
|
||||
List<Book> lb = e.findbyisbn(m.getIsbn());
|
||||
Iterator<Book>ib = lb.iterator();
|
||||
if (ib.hasNext()){
|
||||
Book b = ib.next();
|
||||
b.setPrice(m.getPrice());
|
||||
e.update_book(b);
|
||||
}
|
||||
} catch (JMSException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
18
exercise2/RemoteInterface.java
Normal file
18
exercise2/RemoteInterface.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package exercise2;
|
||||
|
||||
import exercise1.Student;
|
||||
|
||||
import javax.ejb.Remote;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 24/05/2015.
|
||||
*/
|
||||
@Remote
|
||||
public interface RemoteInterface {
|
||||
|
||||
@NotNull List<Book> findbyisbn(@NotNull String in_isbn);
|
||||
List<Book> findall() ;
|
||||
|
||||
}
|
||||
44
exercise2_client/ClientPrintAll.java
Normal file
44
exercise2_client/ClientPrintAll.java
Normal file
@@ -0,0 +1,44 @@
|
||||
package exercise2_client;
|
||||
|
||||
import exercise2.Book;
|
||||
import exercise2.RemoteInterface;
|
||||
|
||||
import javax.naming.Context;
|
||||
import javax.naming.InitialContext;
|
||||
import javax.naming.NamingException;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
public class ClientPrintAll {
|
||||
|
||||
|
||||
public static void main (String[] args) {
|
||||
|
||||
|
||||
Context ctx = null;
|
||||
try {
|
||||
ctx =new InitialContext();
|
||||
} catch (NamingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
RemoteInterface r = null;
|
||||
try {
|
||||
r =(RemoteInterface) ctx.lookup("java:global/web/EJBBook!exercise2.RemoteInterface");
|
||||
List<Book> l = r.findall();
|
||||
Iterator<Book> it = l.iterator();
|
||||
while (it.hasNext()){
|
||||
Book b = it.next();
|
||||
System.out.print(b.getIsbn() + " " + b.getTitle() + " " + b.getAuthor() + " " +b.getPrice() + " ");
|
||||
}
|
||||
|
||||
} catch (NamingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
37
exercise2_client/ClientUpdatePrice.java
Normal file
37
exercise2_client/ClientUpdatePrice.java
Normal file
@@ -0,0 +1,37 @@
|
||||
package exercise2_client;
|
||||
|
||||
import exercise2.BookMessage;
|
||||
|
||||
import javax.jms.ConnectionFactory;
|
||||
import javax.jms.Destination;
|
||||
import javax.jms.JMSContext;
|
||||
import javax.naming.Context;
|
||||
import javax.naming.InitialContext;
|
||||
import javax.naming.NamingException;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 31/05/2015.
|
||||
*/
|
||||
public class ClientUpdatePrice {
|
||||
|
||||
public static void main(String[] args) throws NamingException {
|
||||
|
||||
|
||||
BookMessage m = new BookMessage("0001",Float.valueOf(10000));
|
||||
|
||||
Context jndiContext = new InitialContext();
|
||||
|
||||
ConnectionFactory connectionFactory = (ConnectionFactory) jndiContext.lookup("jms/javaee7/ConnectionFactory");
|
||||
Destination topic = (Destination) jndiContext.lookup("jms/javaee7/Topic");
|
||||
try (JMSContext jmsContext = connectionFactory.createContext()) {
|
||||
jmsContext.createProducer().send(topic, m);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
51
servlet/Servletexercise2.java
Normal file
51
servlet/Servletexercise2.java
Normal file
@@ -0,0 +1,51 @@
|
||||
package servlet;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 25/04/2015.
|
||||
*/
|
||||
|
||||
@WebServlet(name = "Servletexercise2")
|
||||
public class Servletexercise2 extends HttpServlet {
|
||||
|
||||
|
||||
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
response.setContentType("text/html;charset=UTF-8");
|
||||
try (PrintWriter out = response.getWriter()) {
|
||||
/* TODO output your page here. */
|
||||
|
||||
out.println("<!DOCTYPE html>");
|
||||
out.println("<html>");
|
||||
out.println("<head>");
|
||||
out.println("<title>Servlet xg</title>");
|
||||
out.println("</head>");
|
||||
out.println("<body>");
|
||||
out.println("<h1>Servlet xg at " + request.getContextPath() + "</h1>");
|
||||
|
||||
|
||||
|
||||
|
||||
out.println("</body>");
|
||||
out.println("</html>");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
processRequest(request, response);
|
||||
|
||||
}
|
||||
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
processRequest(request, response);
|
||||
|
||||
}
|
||||
}
|
||||
58
servlet/Servlettest4.java
Normal file
58
servlet/Servlettest4.java
Normal file
@@ -0,0 +1,58 @@
|
||||
package servlet;
|
||||
|
||||
import test3_persistence.store_class;
|
||||
import test3_persistence.test_class;
|
||||
|
||||
import javax.ejb.EJB;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 25/04/2015.
|
||||
*/
|
||||
|
||||
@WebServlet(name = "Servlettest4")
|
||||
public class Servlettest4 extends HttpServlet {
|
||||
|
||||
|
||||
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
response.setContentType("text/html;charset=UTF-8");
|
||||
try (PrintWriter out = response.getWriter()) {
|
||||
/* TODO output your page here. */
|
||||
|
||||
out.println("<!DOCTYPE html>");
|
||||
out.println("<html>");
|
||||
out.println("<head>");
|
||||
out.println("<title>Servlet xg</title>");
|
||||
out.println("</head>");
|
||||
out.println("<body>");
|
||||
out.println("<h1>Servlet xg at " + request.getContextPath() + "</h1>");
|
||||
|
||||
|
||||
|
||||
|
||||
out.println("</body>");
|
||||
out.println("</html>");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
processRequest(request, response);
|
||||
|
||||
}
|
||||
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
processRequest(request, response);
|
||||
|
||||
}
|
||||
}
|
||||
12
test4_jms/GeneralMessage.java
Normal file
12
test4_jms/GeneralMessage.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package test4_jms;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 31/05/2015.
|
||||
*/
|
||||
public class GeneralMessage implements Serializable {
|
||||
|
||||
public String s;
|
||||
|
||||
}
|
||||
30
test4_jms/MDB.java
Normal file
30
test4_jms/MDB.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package test4_jms;
|
||||
|
||||
import javax.ejb.ActivationConfigProperty;
|
||||
import javax.ejb.MessageDriven;
|
||||
import javax.inject.Inject;
|
||||
import javax.jms.JMSException;
|
||||
import javax.jms.Message;
|
||||
import javax.jms.MessageListener;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 31/05/2015.
|
||||
*/
|
||||
@MessageDriven(mappedName = "jms/javaee7/Topic", activationConfig = {
|
||||
@ActivationConfigProperty(propertyName = "messageSelector", propertyValue = "author = 0")
|
||||
})
|
||||
public class MDB implements MessageListener {
|
||||
|
||||
@Inject
|
||||
Logger l;
|
||||
|
||||
public void onMessage(Message message) {
|
||||
try {
|
||||
GeneralMessage m = message.getBody(GeneralMessage.class);
|
||||
l.info("Messaggio: " + m.s);
|
||||
} catch (JMSException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
35
test4_jms_client/client.java
Normal file
35
test4_jms_client/client.java
Normal file
@@ -0,0 +1,35 @@
|
||||
package test4_jms_client;
|
||||
|
||||
import test4_jms.GeneralMessage;
|
||||
|
||||
import javax.jms.ConnectionFactory;
|
||||
import javax.jms.Destination;
|
||||
import javax.jms.JMSContext;
|
||||
import javax.naming.Context;
|
||||
import javax.naming.InitialContext;
|
||||
import javax.naming.NamingException;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 31/05/2015.
|
||||
*/
|
||||
public class client {
|
||||
|
||||
public static void main(String[] args) throws NamingException {
|
||||
|
||||
|
||||
GeneralMessage gm = new GeneralMessage();
|
||||
gm.s = "messaggio del caiser 2";
|
||||
|
||||
Context jndiContext = new InitialContext();
|
||||
|
||||
ConnectionFactory connectionFactory = (ConnectionFactory) jndiContext.lookup("jms/javaee7/ConnectionFactory");
|
||||
Destination topic = (Destination) jndiContext.lookup("jms/javaee7/Topic");
|
||||
try (JMSContext jmsContext = connectionFactory.createContext()) {
|
||||
jmsContext.createProducer().setProperty("author", 0).send(topic,gm);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user