简体   繁体   中英

How to call a @Remote EJB in another client project

I need to be able to call a save() method from this simple swing java app to my web app that is published on the server with the beans I use to save a new Entity type class Persona, that has name, address, email.

Im using wildfly 8.x for my server and have my web app published like this:

23:51:10,641 INFO  [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-3) JNDI bindings for session bean named ContactoDAO in deployment unit deployment "proyectobase.war" are as follows:

java:global/proyectobase/ContactoDAO!edu.ups.appdis.proyectobase.negocio.ContactoDAO
java:app/proyectobase/ContactoDAO!edu.ups.appdis.proyectobase.negocio.ContactoDAO
java:module/ContactoDAO!edu.ups.appdis.proyectobase.negocio.ContactoDAO
java:global/proyectobase/ContactoDAO
java:app/proyectobase/ContactoDAO
java:module/ContactoDAO

This is my ContactoDAO Bean:

package edu.ups.appdis.proyectobase.negocio;

import java.io.Serializable;
import java.util.List;

import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.Query;

import edu.ups.appdis.proyectobase.modelo.Persona;

@Stateless
// @Remote(Serializable.class)
@Remote
public class ContactoDAO implements Serializable {

    // @Inject
    // private Logger log;

    /**
     * 
     */

    @Inject
    private EntityManager em;

    public void save(Persona persona) {
//      if (em.find(Persona.class, persona.getCodiog()) == null) {
            insertar(persona);
//      } else {
//
//          update(persona);
//      }
    }

    public void test() {
        System.out.println("si funciona ");
    }

    public void insertar(Persona persona) {
        em.persist(persona);

    }

    public void update(Persona persona) {
        em.merge(persona);

    }

    public void remove(int codigo) {   
        Persona persona = em.find(Persona.class, codigo);
        em.remove(persona);

    }

    public Persona read(int codigo) {
        System.out.println("insertado objeto persona");
        return em.find(Persona.class, codigo);
    }

    public List<Persona> getContactos() {
        String sql = "SELECT p FROM Persona p";
        Query query = em.createQuery(sql, Persona.class);
        List<Persona> personas = query.getResultList();

        return personas;
    }

}

On my simple client swing app I have it set up like this:

package cliente.gui;

import java.awt.EventQueue;
............
import javax.ejb.EJB;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.security.Security;
import java.util.Hashtable;

import edu.ups.appdis.proyectobase.modelo.Persona;
import edu.ups.appdis.proyectobase.negocio.*;

public class guiPersona {
//  @EJB(lookup = "java:global/proyectobase/ContactoDAO!edu.ups.appdis.proyectobase.negocio.ContactoDAO")
    @EJB(lookup = "java:global/proyectobase/ContactoDAO")
    ContactoDAO contactoDAO;
    private JFrame frame;
    private JTextField textNombre;
    private JTextField textDireccion;
    private JTextField textEmail;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    guiPersona window = new guiPersona();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     * 
     * @throws NamingException
     */
    public guiPersona() {
        initialize();
    }



         ...........



JButton btnGuardar = new JButton("Guardar");
        btnGuardar.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                contactoDAO.test();

                if (textDireccion.getText() != "" && textEmail.getText() != "" && textNombre.getText() != "") {

                    Persona p = new Persona();

                    p.setNombre(textNombre.getText());
                    p.setDireccion(textDireccion.getText());
                    p.setEmail(textEmail.getText());

                    contactoDAO.save(p);

                    textNombre.setText("");
                    textDireccion.setText("");
                    textEmail.setText("");
                }

            }
        });

You can see in the code above I use this to call my bean in the other proyect:

@EJB(lookup = "java:global/proyectobase/ContactoDAO")
ContactoDAO contactoDAO;

And on my button to save the new entry I use this:

                if (textDireccion.getText() != "" && textEmail.getText() != "" && textNombre.getText() != "") {

                    Persona p = new Persona();

                    p.setNombre(textNombre.getText());
                    p.setDireccion(textDireccion.getText());
                    p.setEmail(textEmail.getText());

                    contactoDAO.save(p);

                    textNombre.setText("");
                    textDireccion.setText("");
                    textEmail.setText("");
                }

            }
        });

I also tried using this:

@EJB(lookup="java:global/proyectobase/ContactoDAO!edu.ups.appdis.proyectobase.negocio.ContactoDAO")

ContactoDAO contactoDAO;

I keep getting a null pointer exception on my ContactoDAO but is it maybe because the lookup is not finding anything or Im not using it right, I dont really know. My question is what would be another way of calling my save method from my bean in another simple swing project, or maybe Im missing something else whenever I use the @EJB?

EDIT: This is my beans.xml in case you were wondering.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://xmlns.jcp.org/xml/ns/javaee
        http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd" bean-discovery-mode="all">
</beans>

From the log it seems that your ContactDAO and all its dependencies are correct getting initialized on the server. On the standalone swing client possibly you are missing jboss-ejb-client.properties in your META-INF folder or you can also explicitly set the initial context properties and do the JNDI lookup manually.

SE POST

Also you should make sure to include the jboss-client.jar file in the classpath of swing client project.

Wildfly Developer Guide

If you get a Authentication failed exception then you need to add username/password properties to the InitialContext and run the add-user.sh script on the server

Add User

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM