Kill
Kill

Reputation: 23

Failed to read response with EJB Remote when using wildfly 26

i am facing problem that client cannot read response from server with Class Entity(ETC List, Entity). Also with Objects of type String, int, Double it still works normally.

Project EJBServer

  1. Class Entity: AnimalEntity.java

    public class AnimalEntity {

     private String animalName;
     private String animalCountry;
     private String animaltype;
     private double animalWeigth;
    
     public AnimalEntity(String animalName, String animalCountry, String animaltype, double animalWeigth) {
         super();
         this.animalName = animalName;
         this.animalCountry = animalCountry;
         this.animaltype = animaltype;
         this.animalWeigth = animalWeigth;
     }
     public String getAnimalName() {
         return animalName;
     }
     public void setAnimalName(String animalName) {
         this.animalName = animalName;
     }
     ..............
    

    }

  2. Interface: AnimalServiceRemote.java.

import com.ejb.entity.AnimalEntity;

 public interface AnimalServiceRemote {

    public String findByName(String strName);
    public AnimalEntity Request(String strName); 
}
  1. Bean Class: AnimalService.java
import jakarta.ejb.LocalBean;
import jakarta.ejb.Remote;
import jakarta.ejb.Stateless;
@Stateless
@LocalBean
@Remote(AnimalServiceRemote.class)
public class AnimalService implements AnimalServiceRemote {

    @Override
    public String findByName(String strName) {
        List<AnimalEntity> lst= new ArrayList<AnimalEntity>();
        AnimalEntity ani= new AnimalEntity("Dog", "Japan", "Type1", 30);
        AnimalEntity ani1= new AnimalEntity("Cat", "USA", "Type2", 150);
        AnimalEntity ani2= new AnimalEntity("Fish", "Ger", "Type3", 3);
        lst.add(ani);
        lst.add(ani1);
        lst.add(ani2);
        for (int i = 0; i < lst.size(); i++) {
            if (lst.get(i).getAnimalName().equals(strName)){
                return "Name: "+ lst.get(i).getAnimalName() +" - Country: "+ lst.get(i).getAnimalCountry() +" - Weigth: "+lst.get(i).getAnimalWeigth();
            }
        }
        
        return null;
    }

    @Override
    public AnimalEntity Request(String strName) {
        List<AnimalEntity> lst= new ArrayList<AnimalEntity>();
        AnimalEntity ani= new AnimalEntity("Dog", "Japan", "Type1", 30);
        AnimalEntity ani1= new AnimalEntity("Cat", "USA", "Type2", 150);
        AnimalEntity ani2= new AnimalEntity("Fish", "Ger", "Type3", 3);
        lst.add(ani);
        lst.add(ani1);
        lst.add(ani2);
        AnimalEntity animal= new AnimalEntity();
        for (int i = 0; i < lst.size(); i++) {
            if (lst.get(i).getAnimalName().equals(strName)){
                animal=lst.get(i);
                return animal;
            }
        }
        
        return null;
    }
}

AppClient

  1. Main Class: Myclient.java
import javax.naming.Context;
import javax.naming.InitialContext;
import com.ejb.entity.AnimalEntity;
import com.ejb.remote.AnimalServiceRemote;
public class Myclient {

    public static void main(String[] args) {
        try {
            System.out.println("Proccessing...!" );
            // Setting Config InitContext
            Properties props = new Properties();
            props.put(Context.INITIAL_CONTEXT_FACTORY, "org.wildfly.naming.client.WildFlyInitialContextFactory");
            props.put(Context.PROVIDER_URL, "remote+http://localhost:8080");
            props.put("wildfly.naming.client.ejb.context", true);
            InitialContext ctx = new InitialContext(props);
            AnimalServiceRemote animal= (AnimalServiceRemote) ctx.lookup("ejb:/EJBServer/AnimalService!com.ejb.remote.AnimalServiceRemote");
            
            System.out.println("Lookup completeted..." );
            
            System.out.println(animal.findByName("Cat")); // => It's working normally
            
            
            AnimalEntity ani= new AnimalEntity();
            ani=animal.Request("Fish"); // => error occurs here
            
            ctx.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }

}

with return type as String, Int, Double: it is working normally
with return type List<EntityClass>, EntityClass: it throws an error, I still don't understand why.

Error Message:

16:13:28,617 WARN  [org.jboss.ejb.client.remoting] (default task-1) EJBCLIENT000519: Exception occurred when writing EJB response to invocation 61367 over channel Channel ID 79beb44f (inbound) of Remoting connection 38289bc4 to 127.0.0.1/127.0.0.1:58770 of endpoint "desktop-sanolu2" <1d50c57e>: java.io.NotSerializableException: com.ejb.entity.AnimalEntity
    at [email protected]//org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:274)
    at [email protected]//org.jboss.marshalling.AbstractObjectOutput.writeObject(AbstractObjectOutput.java:58)
    at [email protected]//org.jboss.marshalling.AbstractMarshaller.writeObject(AbstractMarshaller.java:111)
    at [email protected]//org.jboss.ejb.protocol.remote.EJBServerChannel$RemotingInvocationRequest$1.writeInvocationResult(EJBServerChannel.java:986)
    at [email protected]//org.jboss.as.ejb3.remote.AssociationImpl.lambda$receiveInvocationRequest$0(AssociationImpl.java:291)
    at [email protected]//org.jboss.as.ejb3.remote.AssociationImpl.execute(AssociationImpl.java:344)
    at [email protected]//org.jboss.as.ejb3.remote.AssociationImpl.receiveInvocationRequest(AssociationImpl.java:297)
    at [email protected]//org.jboss.ejb.protocol.remote.EJBServerChannel$ReceiverImpl.handleInvocationRequest(EJBServerChannel.java:473)
    at [email protected]//org.jboss.ejb.protocol.remote.EJBServerChannel$ReceiverImpl.handleMessage(EJBServerChannel.java:208)
    at [email protected]//org.jboss.remoting3.remote.RemoteConnectionChannel.lambda$handleMessageData$3(RemoteConnectionChannel.java:432)
    at [email protected]//org.jboss.remoting3.EndpointImpl$TrackingExecutor.lambda$execute$0(EndpointImpl.java:991)
    at [email protected]//org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
    at [email protected]//org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1990)
    at [email protected]//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1486)
    at [email protected]//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1377)
    at [email protected]//org.xnio.XnioWorker$WorkerThreadFactory$1$1.run(XnioWorker.java:1280)
    at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: an exception which occurred:
    in object com.ejb.entity.AnimalEntity@45251eee

please help me. Thank you very much

Upvotes: 2

Views: 976

Answers (1)

jwpol
jwpol

Reputation: 1475

java.io.NotSerializableException: com.ejb.entity.AnimalEntity says all what you have to do, namely you need to implement Serializable interface:

public class AnimalEntity implements Serializable

Upvotes: 1

Related Questions