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
Related
I work with Java 8.
I have about 20 scheduled tasks (so about 20 java class) that execute different process with pdf files. Each task has is own frequency. The frequencies are stored in database. So I can change frequency in database and I don't have to restart my tomcat. This is one of them :
#Service
public class InboundJob implements SchedulingConfigurer {
private static final Logger logger = LoggerFactory.getLogger(InboundJob.class);
#Autowired
InboundService inboundService;
ScheduledTaskRegistrar scheduledTaskRegistrar;
#Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
if (scheduledTaskRegistrar == null) {
scheduledTaskRegistrar = taskRegistrar;
}
if (taskRegistrar.getScheduler() == null) {
taskRegistrar.setScheduler(inboundService.poolScheduler());
}
taskRegistrar.addTriggerTask(
() -> scheduleCron(inboundService.getJobRepo().findByLibelleIgnoreCase("INBOUND").getFrequency()),
t -> {
CronTrigger crontrigger = new CronTrigger(inboundService.getJobRepo().findByLibelleIgnoreCase("INBOUND").getFrequency());
return crontrigger.nextExecutionTime(t);
}
);
}
public void scheduleCron(String cron) {
try {
Job inboundJob = inboundService.getJobRepo().findByLibelleIgnoreCase("INBOUND");
if (inboundJob.getStatus() == null) {
for (Bannette item : inboundService.getBannetteRepo().findByIdaijobAndStatus(inboundJob.getIdaijob(), null)) {
if (!inboundService.isFolderExisting(item.getFolderpath())) {
logger.error("InboundJob - Le répertoire {} n'existe pas ", item.getFolderpath());
} else {
ArrayList<File> filesList = inboundService.getFileList(item.getFolderpath());
if (filesList.size() > 0) {
logger.info("InboundJob: " + filesList.size() + " nouveau(x) fichier(s) dans " + item.getFolderpath());
inboundService.trtList(filesList, item);
}
}
}
}
} catch (Exception ure) {
logger.info("InboundJob - ERROR: " + ExceptionUtils.getStackTrace(ure));
}
}
public void activateScheduler() {
logger.info("Re-Activating Scheduler");
configureTasks(scheduledTaskRegistrar);
}
}
The problem is, if a task runs, the others can't start until it has finished...
Is there a way to make them independant ?
------ edit 2022/04/14 -----
this is additionnal info asked in comment
I use spring boot
the imports in my class are
import java.io.File;
import java.util.ArrayList;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Service;
import com.test.mc.entity.Bannette;
import com.test.mc.entity.Job;
import com.test.mc.job.service.InboundService;
and poolScheduler method in my custom InboundService class
#Bean
public TaskScheduler poolScheduler() {
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.setThreadNamePrefix("ThreadPoolTaskScheduler");
scheduler.setPoolSize(1);
scheduler.initialize();
return scheduler;
}
The aim of this mecanism is to get information about frequency in database. And if I change the frequency in my table, the new value is effective without to restart tomcat server. It works fine !
Finally, this is the main method of the project with spring boot configuration
#SpringBootApplication
#EnableJpaAuditing
#EnableScheduling
#Configuration
#EnableAutoConfiguration
#ComponentScan
public class MyKlinckApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(MyKlinckApplication.class, args);
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(MyKlinckApplication.class);
}
}
tell me if you need more
I am trying to integrate XLLoop from a servlet and trying to run in via HTTP protocol. Below is my code:
XlloopServlet.java
#WebServlet(value = "/FunctionServer", name = "FunctionServer", asyncSupported = true)
public class XlloopServlet extends FunctionServlet {
private static final long serialVersionUID = -3845895326255874126L;
#Override
public void init(final ServletConfig config) throws ServletException {
// Create a function information handler to register our functions
FunctionInformationHandler infoHandler = new FunctionInformationHandler();
// Create a reflection function handler and add the required methods
FunctionHandler handler = new FunctionHandler();
infoHandler.add(handler.getFunctions());
// Set the handlers
CompositeFunctionHandler compositeHandler = new CompositeFunctionHandler();
compositeHandler.add(handler);
compositeHandler.add(infoHandler);
// Setting the function handler in the parent servlet
setHandler(compositeHandler);
}
and my FunctionHandler class which registers the functions:
public class FunctionHandler implements IFunctionHandler, FunctionProvider {
private ReflectFunctionHandler rfh;
public FunctionHandler() {
// Create a reflection function handler and add the Math methods
rfh = new ReflectFunctionHandler();
rfh.addMethods("Math.", Math.class);
rfh.addMethods("Math.", Maths.class);
rfh.addMethods("CSV.", CSV.class);
rfh.addMethods("Reflect.", Reflect.class);
}
#Override
public XLoper execute(IFunctionContext arg0, String arg1, XLoper[] arg2) throws RequestException {
return rfh.execute(arg0, arg1, arg2);
}
#Override
public boolean hasFunction(String arg0) {
return rfh.hasFunction(arg0);
}
#Override
public FunctionInformation[] getFunctions() {
return rfh.getFunctions();
}
public ReflectFunctionHandler getReflectFunctionHandler() {
return rfh;
}
}
My XLLoop ini file is as below:
protocol=http
url=http://localhost:8080/MyApp/FunctionServer
Now, when I try to call a function from my excel, I get a call in the servlet class and everything executes, but functions are not getting executed on the excel file.
Anyone having any idea about how to integrate XLLoop plugin on a webserver like tomcat?
I've just implemented this with JAX-RS and a bit of Spring. I use a REST endpoint to populate the xlloop.ini file with the correct server host/port for the running service and then package up my xlsb, xll and ini file in a zip for clients to download. It's not particularly pretty at the moment but the web.xml and Startup snippets are below.
The thing I haven't spent time on yet is memory management. If a lot of users load a lot of data, I'll need to periodically clean that up, so beware of idle session threads!
Web.xml
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<listener>
<listener-class>com.myapp.web.excel.XLLoopStartup</listener-class>
</listener>
XLLoopStartup.java
public class XLLoopStartup implements ServletContextListener {
public static XLLoopStartup INSTANCE;
private FunctionServer fs;
#Inject
private SomeInjectionThing usefulSpringStuff;
#Override
public void contextDestroyed(ServletContextEvent sce) {
}
#Override
public void contextInitialized(ServletContextEvent sce) {
INSTANCE = this;
// Initialize my Spring stuff
if (sce != null){
WebApplicationContextUtils//
.getRequiredWebApplicationContext(sce.getServletContext())//
.getAutowireCapableBeanFactory()//
.autowireBean(this);
}
Executors.newSingleThreadExecutor().execute(new Runnable() {
#Override
public void run() {
registerConverters();
fs = new FunctionServer(Integer.parseInt(System.getProperty("port.tomcat.xlloop", "10606")));
ReflectFunctionHandler rfh = new ReflectFunctionHandler();
rfh.addMethods(ExcelTrades.CATEGORY, ExcelTrades.class);
rfh.addMethods(ExcelUtils.CATEGORY, ExcelUtils.class);
rfh.addMethods(ExcelPositions.CATEGORY, ExcelPositions.class);
rfh.addMethods(ExcelProducts.CATEGORY, ExcelProducts.class);
// Create a function information handler to register our functions
FunctionInformationHandler firh = new FunctionInformationHandler();
firh.add(rfh.getFunctions());
// Set the handlers
CompositeFunctionHandler cfh = new CompositeFunctionHandler();
cfh.add(rfh);
cfh.add(firh);
DebugFunctionHandler debugFunctionHandler = new DebugFunctionHandler(cfh);
fs.setFunctionHandler(new SecureFunctionHandler(debugFunctionHandler));
try {
fs.run();
}
catch (IOException e) {
e.printStackTrace();
}
}
});
}
// For quick testing
public static void main(String[] args) {
new XLLoopStartup().contextInitialized(null);
}
// Function classes can statically access this instance and get spring things from it
public SomeInjectionThing getThing() {
return usefulSpringStuff;
}
}
ExcelService.java
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import org.apache.commons.io.IOUtils;
import io.swagger.annotations.Api;
#Api("excel")
#Path("/excel")
public class ExcelService {
#Context
UriInfo uri;
#GET
#Path("/download")
#Produces({ MediaType.APPLICATION_OCTET_STREAM })
public Response download() {
StringWriter sw = new StringWriter();
// Create an INI file. We should probably store all default settings in a file and just add the server info to
// it.
sw.write("server=");
sw.write(uri.getBaseUri().getHost());
sw.write(":");
sw.write(System.getProperty("port.tomcat.xlloop", "10605"));
String inifile = sw.toString();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (ZipOutputStream zos = new ZipOutputStream(baos)) {
// Add the ini file to the zip
ZipEntry entry = new ZipEntry("xlloop.ini");
zos.putNextEntry(entry);
zos.write(inifile.getBytes());
zos.closeEntry();
// Add the Excel files
writeFileFromClasspath(zos, "xlloop.xll");
// This is my custom Excel macro sheet with other useful functions for user authentication etc.
writeFileFromClasspath(zos, "xlloop.xlsb");
}
catch (IOException ioe) {
ioe.printStackTrace();
}
return Response.ok(new ByteArrayInputStream(baos.toByteArray()))
.header("Content-Disposition", "attachment; filename=xlloop.zip").build();
}
private void writeFileFromClasspath(ZipOutputStream zos, String filename) throws IOException {
ZipEntry xlFileEntry = new ZipEntry(filename);
zos.putNextEntry(xlFileEntry);
zos.write(IOUtils.toByteArray(ExcelService.class.getClassLoader().getResourceAsStream(filename)));
zos.closeEntry();
}
}
I'm working on a project in which I'm trying to inject dependency into a class but #Autowired annotating does not seem to work for some reason.
I have the class ApplicationServer
package project.eHealth;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashSet;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.databind.ObjectMapper;
import project.eHealth.bussiness.AppointmentDto;
import project.eHealth.bussiness.AppointmentService;
#SpringBootApplication
#Component
public class ApplicationServer {
private static final int PORT = 9001;
private static HashSet<String> names = new HashSet<String>();
#Autowired
private static AppointmentService appointments;
public static void main(String[] args) throws Exception {
SpringApplication.run(ApplicationServer.class, args);
System.out.println("The chat server is running.");
ServerSocket listener = new ServerSocket(PORT);
try {
while (true) {
new Handler(listener.accept()).start();
}
} finally {
listener.close();
}
}
private static class Handler extends Thread {
private String name;
private Socket socket;
private BufferedReader in;
private PrintWriter out;
/**
* Constructs a handler thread, squirreling away the socket.
* All the interesting work is done in the run method.
*/
public Handler(Socket socket) {
this.socket = socket;
}
/**
* Services this thread's client by repeatedly requesting a
* screen name until a unique one has been submitted, then
* acknowledges the name and registers the output stream for
* the client in a global set, then repeatedly gets inputs and
* broadcasts them.
*/
public void run() {
try {
// Create character streams for the socket.
in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
ObjectMapper mapper = new ObjectMapper();
// Request a name from this client. Keep requesting until
// a name is submitted that is not already used. Note that
// checking for the existence of a name and adding the name
// must be done while locking the set of names.
/*while (true) {
out.println("SUBMITNAME");
name = in.readLine();
if (name == null) {
return;
}
synchronized (names) {
if (!names.contains(name)) {
names.add(name);
break;
}
}
}*/
// Now that a successful name has been chosen, add the
// socket's print writer to the set of all writers so
// this client can receive broadcast messages.
// Accept messages from this client and broadcast them.
// Ignore other clients that cannot be broadcasted to.
while (true) {
System.out.println("Aici este controlul!");
String input = in.readLine();
System.out.println(input);
Request request = mapper.readValue(input,Request.class);
if (request.getCommand().equals("getAllAppointments")){
if (appointments == null){
System.out.println("appointmentService is null");
}
else{
List<AppointmentDto> appointmentsList = appointments.getAll();
Response response = new Response();
response.setAction("RETURNED");
response.setData(mapper.writeValueAsString(appointmentsList));
String respObj = mapper.writeValueAsString(response);
out.println(respObj);
}
}
System.out.println(input);
if (input == null) {
return;
}
String[] command = input.split("&");
}
} catch (IOException e) {
System.out.println(e);
} finally {
// This client is going down! Remove its name and its print
// writer from the sets, and close its socket.
if (name != null) {
}
if (out != null) {
}
}
}
}
}
And the class AppointmentService
package project.serverSide.server.bussiness;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import project.serverSide.server.data.Appointment;
import project.serverSide.server.data.AppointmentRepository;
#Component
#Service
public class AppointmentService {
#Autowired
AppointmentRepository appointments;
public AppointmentService(){
}
public List<AppointmentDto> getAll(){
List<Appointment> appointmentsList = appointments.findAll();
List<AppointmentDto> appointmentsDto = new ArrayList<AppointmentDto>();
for (Appointment ap : appointmentsList){
AppointmentDto apDto = new AppointmentDto();
apDto.setDoctorName(new SimpleStringProperty(ap.getDoctorName()));
apDto.setPatientName(new SimpleStringProperty(ap.getPatientName()));
apDto.setDateIssued(new SimpleObjectProperty<LocalDate>(ap.getDateIssued()));
appointmentsDto.add(apDto);
}
return appointmentsDto;
}
}
And AppointmentRepository
package project.serverSide.server.data;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Component;
#Component
public interface AppointmentRepository extends JpaRepository<Appointment,Integer> {
List<Appointment> findAll();
}
The issue is that ApplicationServer class does not inject the AppointmentService for some reason and I do not know why. I've been spending an important number of hours search for an answer but without any result.
Help is deeply appreciated!
You are missing #ComponentScan in your ApplicationServer class.
#SpringBootApplication
#ComponentScan(basePackages = {"project.serverSide"})
public class ApplicationServer {
}
And remove static from AppointmentService:
#Autowired
private AppointmentService appointments;
If you need static instance variable then use setter injection instead, as follows:
private static AppointmentService appointments;
#Autowired
public void setAppointments(AppointmentService appointments){
ApplicationServer.appointments = appointments;
}
you need to add base package in ApplicationContext.xml, u=you can use below code :-
<context:component-scan annotation-config="true"
base-package="com.amstech.mayal" />
below code use for add base package fot repositories:-
<context:annotation-config />
<jpa:repositories base-package="com.amstech.mayal.repository" />
and also add below code for Autowired:-
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
I am learning about EJB and I would like to get the following code working but so far no success.
Here's my EJB project code:
#Stateless
public class CalcBean implements ICalcRemote {
private static final long serialVersionUID = 5571798968598315142L;
#Override
public int add(int a, int b) {
return a + b;
}
}
package com.ejb.test.pckg;
import javax.ejb.Remote;
#Remote
public interface ICalcRemote extends ICalculator {
}
package com.ejb.test.pckg;
import java.io.Serializable;
public interface ICalculator extends Serializable {
public int add(int a, int b);
}
I run glassfish-4.1.1 in Eclipse Neon.
When I deploy the EJB project, I can see the following in the log:
2017-02-24T21:18:09.036-0400|Info: Portable JNDI names for EJB CalcBean: [java:global/EJBDemo/CalcBean, java:global/EJBDemo/CalcBean!com.ejb.test.pckg.ICalcRemote]
2017-02-24T21:18:09.036-0400|Info: Glassfish-specific (Non-portable) JNDI names for EJB CalcBean: [com.ejb.test.pckg.ICalcRemote#com.ejb.test.pckg.ICalcRemote, com.ejb.test.pckg.ICalcRemote]
This is my client code:
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import com.ejb.test.pckg.ICalcRemote;
public class Main {
public static void main(String[] args) {
try {
Properties props = new Properties();
props.setProperty("java.naming.factory.initial", "com.sun.enterprise.naming.SerialInitContextFactory");
props.setProperty("java.naming.factory.url.pkgs", "com.sun.enterprise.naming");
props.setProperty("java.naming.factory.state", "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
//
props.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
props.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
Context ctx = new InitialContext();
ICalcRemote calc = (ICalcRemote) ctx.lookup("java:global/EJBDemo/CalcBean!com.ejb.test.pckg.ICalcRemote");
System.out.println(calc.add(5, 7));
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* (non-Java-doc)
*
* #see java.lang.Object#Object()
*/
public Main() {
super();
}
}
But I am having no luck. Any suggestions how to get this working?
Thank you!
EDIT:
This is my main (client) which includes info from the EJBDemo deployment log:
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import com.ejb.test.pckg.ICalcRemote;
public class Main {
public static void main(String[] args) {
try {
Properties props = new Properties();
props.setProperty("java.naming.factory.initial", "com.sun.enterprise.naming.SerialInitContextFactory");
props.setProperty("java.naming.factory.url.pkgs", "com.sun.enterprise.naming");
props.setProperty("java.naming.factory.state", "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
//
// props.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
// props.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
/*
* THIS IS from Glassfish log of EJBDemo deployment
*
* 2017-02-25T20:41:47.100-0400|Info: Portable JNDI names for EJB CalcBean: [java:global/EJBDemo/CalcBean,
* java:global/EJBDemo/CalcBean!com.ejb.test.pckg.ICalcRemote] 2017-02-25T20:41:47.100-0400|Info: Glassfish-specific (Non-portable) JNDI names for EJB CalcBean:
* [com.ejb.test.pckg.ICalcRemote#com.ejb.test.pckg.ICalcRemote, com.ejb.test.pckg.ICalcRemote]
*
*
*/
Context ctx = new InitialContext();
ICalcRemote calc = (ICalcRemote) ctx.lookup("java:global/EJBDemo/CalcBean!com.ejb.test.pckg.ICalcRemote");
System.out.println(calc.add(5, 7));
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* (non-Java-doc)
*
* #see java.lang.Object#Object()
*/
public Main() {
super();
}
}
I finally got things working! Since it's been quite a process despite reading many posts related to this issue. Here are the details of my configuration. Hopefully, this will be useful to somebody.
OS: win 10
IDE: Eclipse Neon
App server: Glassfish 4.1.1
JDK: 1.8.0_111
I need to mention this article which eventually led me to the answer:
http://mavicode.com/2014/08/a-standalone-client-for-ejbs-running-on-glassfish-4/
So, thanks and kudos.
First, create the EJB Demo project:
package com.ejb.test.pckg;
import java.io.Serializable;
public interface ICalculator extends Serializable {
public int add(int a, int b);
}
package com.ejb.test.pckg;
import javax.ejb.Remote;
#Remote
public interface ICalcRemote extends ICalculator {
}
package com.ejb.test.pckg;
import javax.ejb.Stateless;
// #Stateless(mappedName = "chester")
#Stateless
public class CalcBean implements ICalcRemote {
private static final long serialVersionUID = 5571798968598315142L;
#Override
public int add(int a, int b) {
return a + b;
}
}
Deploy it on the server (run as > run on the server) > check the log to see the JDNI info. It should look like something like this:
2017-02-25T20:41:47.100-0400|Info: Portable JNDI names for EJB
CalcBean: [java:global/EJBDemo/CalcBean,
java:global/EJBDemo/CalcBean!com.ejb.test.pckg.ICalcRemote]
2017-02-25T20:41:47.100-0400|Info: Glassfish-specific (Non-portable)
JNDI names for EJB CalcBean:
[com.ejb.test.pckg.ICalcRemote#com.ejb.test.pckg.ICalcRemote,
com.ejb.test.pckg.ICalcRemote]
After that, create Application Client Project. Main.java will be created automatically. This is my main:
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import com.ejb.test.pckg.ICalcRemote;
public class Main {
public static void main(String[] args) {
try {
Properties props = new Properties();
props.setProperty("java.naming.factory.initial", "com.sun.enterprise.naming.SerialInitContextFactory");
props.setProperty("java.naming.factory.url.pkgs", "com.sun.enterprise.naming");
props.setProperty("java.naming.factory.state", "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
Context ctx = new InitialContext();
ICalcRemote calc = (ICalcRemote) ctx.lookup("java:global/EJBDemo/CalcBean!com.ejb.test.pckg.ICalcRemote");
System.out.println(calc.add(5, 43));
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* (non-Java-doc)
*
* #see java.lang.Object#Object()
*/
public Main() {
super();
}
}
At this point, my project looks like this:
And here comes the key part. Add a new external library from the Glassfish lib directory. !!! IMPORTANT: Don't just copy and paste to the path! The .jar apparently uses/references other classes in other GF jars. So just add it to the build path as an external jar instead of copy/paste.
You should be set now. Run the Main as a Java application.
This is what I get.
I try to run a websocket server in a Java project that was running on Tomcat6. I have set up a Tomcat 7 server where the project now is running on.
First I tried to run the socket example of Tomcat7. This run perfectly. I copied this class to my old project. When I run the old project again all the functionalities are working like before but only the websocket server doe not work.
This is the ChatAnnotation class that I have copied from the examples from Tomcat to my old project.
import java.io.IOException;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicInteger;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import org.apache.log4j.Logger;
#ServerEndpoint(value = "/websocket/chat")
public class ChatAnnotation {
private static Logger logger = Logger.getLogger(ChatAnnotation.class);
private static final String GUEST_PREFIX = "Guest";
private static final AtomicInteger connectionIds = new AtomicInteger(0);
private static final Set<ChatAnnotation> connections = new CopyOnWriteArraySet<ChatAnnotation>();
private final String nickname;
private Session session;
public ChatAnnotation() {
nickname = GUEST_PREFIX + connectionIds.getAndIncrement();
logger.info("ws instance");
}
#OnOpen
public void start(Session session) {
this.session = session;
connections.add(this);
String message = String.format("* %s %s", nickname, "has joined.");
broadcast(message);
}
#OnClose
public void end() {
connections.remove(this);
String message = String.format("* %s %s", nickname, "has disconnected.");
broadcast(message);
}
#OnMessage
public void incoming(String message) {
// Never trust the client
String filteredMessage = String.format("%s: %s", nickname, message.toString());
broadcast(filteredMessage);
}
#OnError
public void onError(Throwable t) throws Throwable {
logger.error("Chat Error: " + t.toString(), t);
}
private static void broadcast(String msg) {
for (ChatAnnotation client : connections) {
try {
synchronized (client) {
client.session.getBasicRemote().sendText(msg);
}
} catch (IOException e) {
logger.debug("Chat Error: Failed to send message to client", e);
connections.remove(client);
try {
client.session.close();
} catch (IOException e1) {
// Ignore
}
String message = String.format("* %s %s", client.nickname, "has been disconnected.");
broadcast(message);
}
}
}
}
I have noting added in my web.xml. In my old project are also tcpsockets used can this be the problem?
Can anyone help me with this problem?
EDIT
Class added:
import java.util.HashSet;
import java.util.Set;
import javax.websocket.Endpoint;
import javax.websocket.server.ServerApplicationConfig;
import javax.websocket.server.ServerEndpointConfig;
import org.apache.log4j.Logger;
public class ExamplesConfig implements ServerApplicationConfig {
private static Logger log = Logger.getLogger(ChatAnnotation.class);
public Set<ServerEndpointConfig> getEndpointConfigs(Set<Class<? extends Endpoint>> endpointClasses) {
Set<ServerEndpointConfig> result = new HashSet<ServerEndpointConfig>();
log.info("getEndpointConfigs");
return result;
}
public Set<Class<?>> getAnnotatedEndpointClasses(Set<Class<?>> scanned) {
log.info("getAnnotatedEndpointClasses");
return scanned;
}
}
Java websocket server use return value of ServerApplicationConfig interface to deploy programmatic endpoints and for annotated endpoints.
For Tomcat example, if you change the package name of ChatAnnotation. You have to modify websocket.ExamplesConfig too.
public Set<Class<?>> getAnnotatedEndpointClasses(Set<Class<?>> scanned) {
// Deploy all WebSocket endpoints defined by annotations in the examples
// web application. Filter out all others to avoid issues when running
// tests on Gump
Set<Class<?>> results = new HashSet<>();
for (Class<?> clazz : scanned) {
String name = clazz.getPackage().getName();
boolean ok = name.startsWith("websocket.");
if (ok) {
results.add(clazz);
}
}
return scanned;
}
The getAnnotatedEndpointClasses(scanned) only return classes which package name start with websocket. Unmatched classes will not deployed even they have #ServerEndpoint declarations.