esercizi webservices wsdl
This commit is contained in:
73
exam/CD.java
Normal file
73
exam/CD.java
Normal file
@@ -0,0 +1,73 @@
|
||||
package exam;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.NamedQueries;
|
||||
import javax.persistence.NamedQuery;
|
||||
import javax.validation.constraints.DecimalMin;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 16/06/2015.
|
||||
*/
|
||||
|
||||
@Entity
|
||||
@NamedQueries({
|
||||
@NamedQuery(name = "findid", query = "SELECT c FROM CD c WHERE c.id =?1"),
|
||||
@NamedQuery(name = "findauthor", query = "SELECT c FROM CD c WHERE c.author =?1"),
|
||||
@NamedQuery(name = "findall", query = "SELECT c FROM CD c"),
|
||||
})
|
||||
|
||||
public class CD implements Serializable {
|
||||
|
||||
@NotNull @Id
|
||||
private String id;
|
||||
@NotNull
|
||||
private String title;
|
||||
@NotNull
|
||||
private String author;
|
||||
@NotNull @DecimalMin("0.0")
|
||||
private Float price;
|
||||
|
||||
public CD (){}
|
||||
|
||||
public CD (@NotNull String id_in,@NotNull String title_in,@NotNull String author_in,@NotNull @DecimalMin("0.0") Float price_in){
|
||||
|
||||
id = id_in;
|
||||
title = title_in;
|
||||
author = author_in;
|
||||
price = price_in;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public @NotNull String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
|
||||
public @NotNull String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(@NotNull String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public @NotNull @DecimalMin("0.0") Float getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(@NotNull @DecimalMin("0.0") Float price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public @NotNull String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setAuthor(@NotNull String author) {
|
||||
this.author = author;
|
||||
}
|
||||
}
|
||||
27
exam/ClientPrintAllCDs.java
Normal file
27
exam/ClientPrintAllCDs.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package exam;
|
||||
|
||||
import javax.naming.Context;
|
||||
import javax.naming.InitialContext;
|
||||
import javax.naming.NamingException;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
public class ClientPrintAllCDs {
|
||||
|
||||
|
||||
public static void main (String[] args) throws NamingException{
|
||||
// added method main and signature exception
|
||||
|
||||
Context ctx =new InitialContext();
|
||||
|
||||
RemoteEJB r = (RemoteEJB) ctx.lookup("java:global/web/EJB!exam.RemoteEJB");
|
||||
List<CD> l = r.findall();
|
||||
Iterator<CD> i = l.iterator();
|
||||
while (i.hasNext()){
|
||||
CD temp = i.next();
|
||||
System.out.print(temp.getId() + " " + temp.getTitle() + " " + temp.getAuthor() + " " +temp.getPrice() + "\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
34
exam/ClientUpdatePrice.java
Normal file
34
exam/ClientUpdatePrice.java
Normal file
@@ -0,0 +1,34 @@
|
||||
package exam;
|
||||
|
||||
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 {
|
||||
//removed try catch e added signatute exception to main method
|
||||
|
||||
Context ctx = new InitialContext();
|
||||
|
||||
ConnectionFactory cf = (ConnectionFactory) ctx.lookup("jms/javaee7/ConnectionFactory");
|
||||
Destination d = (Destination) ctx.lookup("jms/javaee7/Topic");
|
||||
try (JMSContext jmsContext = cf.createContext()) {
|
||||
GeneralMessage gm = new GeneralMessage(args[0],Float.valueOf(args[1]));
|
||||
jmsContext.createProducer().send(d, gm);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
40
exam/DatabaseInitializer.java
Normal file
40
exam/DatabaseInitializer.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package exam;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.PreDestroy;
|
||||
import javax.ejb.Singleton;
|
||||
import javax.ejb.Startup;
|
||||
import javax.inject.Inject;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 16/06/2015.
|
||||
*/
|
||||
|
||||
@Singleton
|
||||
@Startup
|
||||
|
||||
public class DatabaseInitializer {
|
||||
|
||||
@Inject
|
||||
EJB e;
|
||||
|
||||
private CD first;
|
||||
private CD second;
|
||||
|
||||
@PostConstruct
|
||||
protected void initialize(){
|
||||
first = new CD("001","Title1","Author1",35.0f);
|
||||
e.createCD(first);
|
||||
second = new CD("002","Title2","Author2",40.0f);
|
||||
e.createCD(second);
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
protected void deleteall(){
|
||||
|
||||
e.removeCD(first);
|
||||
e.removeCD(second);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
58
exam/EJB.java
Normal file
58
exam/EJB.java
Normal file
@@ -0,0 +1,58 @@
|
||||
package exam;
|
||||
|
||||
import javax.ejb.LocalBean;
|
||||
import javax.ejb.Stateless;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import javax.persistence.Query;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 16/06/2015.
|
||||
*/
|
||||
|
||||
@Stateless
|
||||
@LocalBean
|
||||
|
||||
public class EJB implements RemoteEJB{
|
||||
|
||||
@PersistenceContext(unitName = "exam")
|
||||
EntityManager em ;
|
||||
|
||||
|
||||
public void createCD (CD c){
|
||||
em.persist(c);
|
||||
}
|
||||
|
||||
public void updateCD (CD c) {
|
||||
em.merge(c);
|
||||
em.persist(c);
|
||||
}
|
||||
|
||||
public void removeCD (CD c){
|
||||
em.merge(c);
|
||||
em.remove(c);
|
||||
}
|
||||
|
||||
|
||||
public List<CD> findall() {
|
||||
Query q = em.createNamedQuery("findall");
|
||||
return (List<CD>) q. getResultList();
|
||||
|
||||
}
|
||||
|
||||
|
||||
public List<CD> findauthor(String author_in) {
|
||||
Query q = em.createNamedQuery("findauthor");
|
||||
q.setParameter(1,author_in);
|
||||
return (List<CD>) q. getResultList();
|
||||
}
|
||||
|
||||
|
||||
public List<CD> findid(String id_in) {
|
||||
Query q = em.createNamedQuery("findid");
|
||||
q.setParameter(1,id_in);
|
||||
return (List<CD>) q. getResultList();
|
||||
}
|
||||
}
|
||||
25
exam/GeneralMessage.java
Normal file
25
exam/GeneralMessage.java
Normal file
@@ -0,0 +1,25 @@
|
||||
package exam;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 16/06/2015.
|
||||
*/
|
||||
public class GeneralMessage implements Serializable{
|
||||
|
||||
private String id;
|
||||
private Float price;
|
||||
|
||||
public GeneralMessage (String in_id, Float in_price){
|
||||
id = in_id;
|
||||
price = in_price;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public Float getPrice() {
|
||||
return price;
|
||||
}
|
||||
}
|
||||
43
exam/MDB.java
Normal file
43
exam/MDB.java
Normal file
@@ -0,0 +1,43 @@
|
||||
package exam;
|
||||
|
||||
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 16/06/2015.
|
||||
*/
|
||||
|
||||
@MessageDriven(mappedName = "jms/javaee7/Topic")
|
||||
public class MDB implements MessageListener {
|
||||
|
||||
@Inject
|
||||
EJB e;
|
||||
|
||||
|
||||
public void onMessage(Message m) {
|
||||
|
||||
try {
|
||||
GeneralMessage gm = m.getBody(GeneralMessage.class);
|
||||
List<CD> l = e.findid(gm.getId());
|
||||
Iterator<CD> i = l.iterator();
|
||||
if (i.hasNext()){
|
||||
CD temp = i.next();
|
||||
temp.setPrice(gm.getPrice());
|
||||
e.updateCD(temp);
|
||||
}
|
||||
|
||||
} catch (JMSException e) { // added try catch for JMSException
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
16
exam/RemoteEJB.java
Normal file
16
exam/RemoteEJB.java
Normal file
@@ -0,0 +1,16 @@
|
||||
package exam;
|
||||
|
||||
import javax.ejb.Remote;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 16/06/2015.
|
||||
*/
|
||||
@Remote
|
||||
public interface RemoteEJB {
|
||||
|
||||
List<CD> findall();
|
||||
List<CD> findauthor(String author_in);
|
||||
List<CD> findid(String id_in);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user