Skip to content

Instantly share code, notes, and snippets.

@steppat
Created August 13, 2014 23:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steppat/17f8db325d923c17cc5e to your computer and use it in GitHub Desktop.
Save steppat/17f8db325d923c17cc5e to your computer and use it in GitHub Desktop.
jndi.properties
----------------
java.naming.factory.url.pkgs=org.jboss.ejb.client.naming
jboss-ejb-client.properties (Usuario deve ser criado antes através do add-user.sh)
----------------------------
remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false
remote.connections=default
remote.connection.default.host=localhost
remote.connection.default.port = 4447
remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false
remote.connection.two.host=localhost
remote.connection.two.port = 4447
remote.connection.two.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false
remote.connection.default.username=caelum
remote.connection.default.password=caelum
ClienteCarrinhoLivraria.java
-------------------------------------------
public class ClienteCarrinhoLivraria {
public static void main(String[] args) throws NamingException {
InitialContext ic = new InitialContext();
Carrinho carrinho = (Carrinho) ic.lookup("ejb:/livraria/" +
"CarrinhoBean!br.com.caelum.livraria.ejb.Carrinho?stateful");
Livro livro = new Livro();
livro.setNome("Alice no País das Maravilhas");
livro.setPreco(15.0);
carrinho.addLivro(livro);
System.out.println(carrinho.getTotal());
carrinho.finalizaCompra();
}
}
CarrinhoBean.java
-----------------------
@Stateful
@Remote(Carrinho.class)
public class CarrinhoBean implements Carrinho {
private List<Livro> livros = new ArrayList<Livro>();
private double total;
public void addLivro(Livro livro) {
System.out.println("Adicionando o livro " + livro.getNome() + " ao carrinho.");
this.livros.add(livro);
this.total += livro.getPreco();
}
public List<Livro> getLivros() {
System.out.println("Carrinho devolvendo a lista de livros: ");
return this.livros;
}
public double getTotal() {
System.out.println("Carrinho devolvendo o total: " + total);
return this.total;
}
public void finalizaCompra() {
System.out.println("Finalizando a compra de: ");
for (Livro livro : this.livros) {
System.out.println(livro.getPreco() + " - " + livro.getNome());
}
}
}
Carrinho.java
------------------------------------------
public interface Carrinho {
void addLivro(Livro livro);
List<Livro> getLivros();
double getTotal();
void finalizaCompra();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment