esercizio e test usando jms e mdb

This commit is contained in:
2015-05-31 21:15:23 +02:00
parent b4e795d64a
commit 620edb5049
15 changed files with 517 additions and 3 deletions

71
exercise2/Book.java Normal file
View 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;
}
}

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

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

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