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
Related
I am newbie to Spring Integration. I am working on solution, but I am stuck on a specific issue while using inbound file adapter ( FileReadingMessageSource ).
I have to read files from different directories and process them and save the files in different directories. As I understand, the directory name is fixed at the start of the flow.
Can some one help me on changing the directory name for different requests.
I attempted the following. First of all, I am not sure whether it is correct way to about and although it worked for only one directory. I think Poller was waiting for more files and never came back to read another directory.
#SpringBootApplication
#EnableIntegration
#IntegrationComponentScan
public class SiSampleFileProcessor {
#Autowired
MyFileProcessor myFileProcessor;
#Value("${si.outdir}")
String outDir;
#Autowired
Environment env;
public static void main(String[] args) throws IOException {
ConfigurableApplicationContext ctx = new SpringApplication(SiSampleFileProcessor.class).run(args);
FileProcessingService gateway = ctx.getBean(FileProcessingService.class);
boolean process = true;
while (process) {
System.out.println("Please enter the input Directory: ");
String inDir = new Scanner(System.in).nextLine();
if ( inDir.isEmpty() || inDir.equals("exit") ) {
process=false;
} else {
System.out.println("Processing... " + inDir);
gateway.processFilesin(inDir);
}
}
ctx.close();
}
#MessagingGateway(defaultRequestChannel="requestChannel")
public interface FileProcessingService {
String processFilesin( String inputDir );
}
#Bean(name = PollerMetadata.DEFAULT_POLLER)
public PollerMetadata poller() {
return Pollers.fixedDelay(1000).get();
}
#Bean
public MessageChannel requestChannel() {
return new DirectChannel();
}
#ServiceActivator(inputChannel = "requestChannel")
#Bean
GenericHandler<String> fileReader() {
return new GenericHandler<String>() {
#Override
public Object handle(String p, Map<String, Object> map) {
FileReadingMessageSource fileSource = new FileReadingMessageSource();
fileSource.setDirectory(new File(p));
Message<File> msg;
while( (msg = fileSource.receive()) != null ) {
fileInChannel().send(msg);
}
return null; // Not sure what to return!
}
};
}
#Bean
public MessageChannel fileInChannel() {
return MessageChannels.queue("fileIn").get();
}
#Bean
public IntegrationFlow fileProcessingFlow() {
return IntegrationFlows.from(fileInChannel())
.handle(myFileProcessor)
.handle(Files.outboundAdapter(new File(outDir)).autoCreateDirectory(true).get())
.get();
}
}
EDIT: Based on Gary's response replaced some methods as
#MessagingGateway(defaultRequestChannel="requestChannel")
public interface FileProcessingService {
boolean processFilesin( String inputDir );
}
#ServiceActivator(inputChannel = "requestChannel")
public boolean fileReader(String inDir) {
FileReadingMessageSource fileSource = new FileReadingMessageSource();
fileSource.setDirectory(new File(inDir));
fileSource.afterPropertiesSet();
fileSource.start();
Message<File> msg;
while ((msg = fileSource.receive()) != null) {
fileInChannel().send(msg);
}
fileSource.stop();
System.out.println("Sent all files in directory: " + inDir);
return true;
}
Now it is working as expected.
You can use this code
FileProcessor.java
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;
#Component
public class FileProcessor {
private static final String HEADER_FILE_NAME = "file_name";
private static final String MSG = "%s received. Content: %s";
public void process(Message<String> msg) {
String fileName = (String) msg.getHeaders().get(HEADER_FILE_NAME);
String content = msg.getPayload();
//System.out.println(String.format(MSG, fileName, content));
System.out.println(content);
}
}
LastModifiedFileFilter.java
package com.example.demo;
import org.springframework.integration.file.filters.AbstractFileListFilter;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
public class LastModifiedFileFilter extends AbstractFileListFilter<File> {
private final Map<String, Long> files = new HashMap<>();
private final Object monitor = new Object();
#Override
protected boolean accept(File file) {
synchronized (this.monitor) {
Long previousModifiedTime = files.put(file.getName(), file.lastModified());
return previousModifiedTime == null || previousModifiedTime != file.lastModified();
}
}
}
Main Class= DemoApplication.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import org.apache.commons.io.FileUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.Aggregator;
import org.springframework.integration.annotation.InboundChannelAdapter;
import org.springframework.integration.annotation.Poller;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.core.MessageSource;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.dsl.IntegrationFlows;
import org.springframework.integration.dsl.channel.MessageChannels;
import org.springframework.integration.dsl.core.Pollers;
import org.springframework.integration.file.FileReadingMessageSource;
import org.springframework.integration.file.filters.CompositeFileListFilter;
import org.springframework.integration.file.filters.SimplePatternFileListFilter;
import org.springframework.integration.file.transformer.FileToStringTransformer;
import org.springframework.integration.scheduling.PollerMetadata;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.PollableChannel;
import org.springframework.stereotype.Component;
#SpringBootApplication
#Configuration
public class DemoApplication {
private static final String DIRECTORY = "E:/usmandata/logs/input/";
public static void main(String[] args) throws IOException, InterruptedException {
SpringApplication.run(DemoApplication.class, args);
}
#Bean
public IntegrationFlow processFileFlow() {
return IntegrationFlows
.from("fileInputChannel")
.transform(fileToStringTransformer())
.handle("fileProcessor", "process").get();
}
#Bean
public MessageChannel fileInputChannel() {
return new DirectChannel();
}
#Bean
#InboundChannelAdapter(value = "fileInputChannel", poller = #Poller(fixedDelay = "1000"))
public MessageSource<File> fileReadingMessageSource() {
CompositeFileListFilter<File> filters =new CompositeFileListFilter<>();
filters.addFilter(new SimplePatternFileListFilter("*.log"));
filters.addFilter(new LastModifiedFileFilter());
FileReadingMessageSource source = new FileReadingMessageSource();
source.setAutoCreateDirectory(true);
source.setDirectory(new File(DIRECTORY));
source.setFilter(filters);
return source;
}
#Bean
public FileToStringTransformer fileToStringTransformer() {
return new FileToStringTransformer();
}
#Bean
public FileProcessor fileProcessor() {
return new FileProcessor();
}
}
The FileReadingMessageSource uses a DirectoryScanner internally; it is normally set up by Spring after the properties are injected. Since you are managing the object outside of Spring, you need to call Spring bean initialization and lifecycle methods afterPropertiesSet() , start() and stop().
Call stop() when the receive returns null.
> return null; // Not sure what to return!
If you return nothing, your calling thread will hang in the gateway waiting for a response. You could change the gateway to return void or, since your gateway is expecting a String, just return some value.
However, your calling code is not looking at the result anyway.
> gateway.processFilesin(inDir);
Also, remove the #Bean from the #ServiceActivator; with that style, the bean type must be MessageHandler.
Following is code
I am using spring.boot.version 1.4.1.RELEASE now
Nothing is printed when I start the server
I am using #Scheduled annotation to run a cron job but It never starts
Same code works fine if I create new project and use following classes
Please suggest what can possibly go wrong ?
package com.equilar.bsp;
import java.util.TimeZone;
import javax.annotation.PreDestroy;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.boot.orm.jpa.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.scheduling.annotation.EnableScheduling;
import com.amazonaws.http.IdleConnectionReaper;
import com.cloudinary.Cloudinary;
import com.equilar.bsp.config.RedisConfig;
import com.equilar.bsp.config.SecurityConfig;
import com.equilar.bsp.mvc.MvcConfig;
import com.equilar.bsp.util.JwtTokenGenerator;
import com.equilar.bsp.util.Util;
#SpringBootApplication
#EnableScheduling
#Configuration
#EnableAutoConfiguration
#EnableJpaAuditing
//#ComponentScan(basePackages = "com.equilar" ,lazyInit = true)
#EnableJpaRepositories("com.equilar")
#EntityScan({"com.equilar.bsp.domain", "com.equilar.newcommon.folder.domain", "com.equilar.newcommon.pdf.domain"})
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
// set default timezone first thing!!
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
SpringApplication.run(Application.class, args);
JwtTokenGenerator.getStartTime();
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(applicationClass, SecurityConfig.class, MvcConfig.class, RedisConfig.class);
}
private static Class<Application> applicationClass = Application.class;
#PreDestroy
private void cleanUp() {
/* try {
// Shutting down AWS IdleConnectionReaper thread...
IdleConnectionReaper.shutdown();
List<Thread> threadsList = getThreadByName("logback-loggly-appender");
if(!Util.isNullOrEmptyCollection(threadsList)){
for (Thread thread : threadsList) {
thread.interrupt();
}
}
} catch (Throwable t) {
// log error
t.printStackTrace();
}*/
}
/*public List<Thread> getThreadByName(String threadName) {
List<Thread> threads = new ArrayList<Thread>();
for (Thread t : Thread.getAllStackTraces().keySet()) {
if (t.getName().equals(threadName)){
threads.add(t);
}
}
return threads;
}*/
#Value("${CLOUDINARY_URL}")
private String cloudinaryUrl;
#Bean(name = "cloudinary")
public Cloudinary Instance() {
return new Cloudinary(cloudinaryUrl);
}
}
package com.equilar.bsp.calc;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
#org.springframework.stereotype.Component
public class Component {
private Logger logger = LoggerFactory.getLogger(this.getClass());
#Scheduled(
cron = "0,30 * * * * *")
public void cronJob() {
logger.info("> cronJob");
logger.info("\n\n>>>>>>>>>>>>>>>>>>>>>>>>>> In Chron Job."
);
logger.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>> ");
}
}
Your code sample looks totally fine for me. Moreover, I've created a project with sample code you've provided and it worked( with spring.boot.version 1.2.1.RELEASE ).
There is a similar project on github you may be interested in.
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.
Hi i have an issue with spring websockets, this is the scenario:
a standalone application is sending (remote) some data like date Date, procedence String, and weight BigDecimal this data is sending via TCP to socket,
after that this data is saving into database at this point all is fine, but in the next step (websocket) i cannot show this information in a webpage, the weight data must be showed (live) in the screen
this is my websocket configuration:
import java.util.List;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.converter.MessageConverter;
import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver;
import org.springframework.messaging.handler.invocation.HandlerMethodReturnValueHandler;
import org.springframework.messaging.simp.config.ChannelRegistration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketTransportRegistration;
#Configuration
#EnableWebSocketMessageBroker
public class WebSocketConfiguration implements WebSocketMessageBrokerConfigurer {
#Override
public void registerStompEndpoints(final StompEndpointRegistry registry) {
registry.addEndpoint("/indicator").withSockJS();
}
#Override
public void configureClientInboundChannel(final ChannelRegistration registration) {
}
#Override
public void configureClientOutboundChannel(final ChannelRegistration registration) {
}
#Override
public void configureMessageBroker(final MessageBrokerRegistry registry) {
}
#Override
public void configureWebSocketTransport(WebSocketTransportRegistration wstr) {
}
#Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> list) {
}
#Override
public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> list) {
}
#Override
public boolean configureMessageConverters(List<MessageConverter> list) {
return Boolean.TRUE;
}
}
this is my another class that receive data form socket and process information and send to websocket:
import com.mcss.mcontrols.helper.ByteHelper;
import com.spc.basweb.Constants;
import com.spc.basweb.transmissor.dto.Transmission;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.messaging.core.MessageSendingOperations;
import org.springframework.messaging.simp.broker.BrokerAvailabilityEvent;
import com.spc.basweb.service.BroadcastingService;
import com.spc.basweb.service.DataProcessorService;
import java.io.IOException;
import org.springframework.integration.annotation.MessageEndpoint;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.annotation.Transformer;
#MessageEndpoint
public class BroadcastingServiceImpl implements BroadcastingService, ApplicationListener<BrokerAvailabilityEvent> {
private static final Logger LOGGER = Logger.getLogger(BroadcastingServiceImpl.class);
private final MessageSendingOperations<String> messagingTemplate;
private String processedData;
#Autowired
DataProcessorService dataProcessorService;
#Autowired
public BroadcastingServiceImpl(final MessageSendingOperations<String> messagingTemplate) {
this.messagingTemplate = messagingTemplate;
}
#Override
public String getProcessedData() {
return processedData;
}
#Override
#ServiceActivator(inputChannel = "broadcaster")
public String broadcast(byte[] bytes) {
try {
Transmission t = (Transmission) ByteHelper.toObject(bytes);
LOGGER.debug(t.getProcedence() + " " + t.getDate() + " " + t.getWeight());
String rm = this.dataProcessorService.processData(t);
this.messagingTemplate.convertAndSend(Constants.END_POINT_READ, this.dataProcessorService.getWeighing().getWeight().toString());
return rm;
} catch (IOException | ClassNotFoundException ex) {
LOGGER.error("Error de transmision de objetos", ex);
}
return DataProcessorService.NOT_OK_RESPONSE;
}
#Override
public void onApplicationEvent(BrokerAvailabilityEvent e) {
LOGGER.debug("Application event");
}
#Transformer(outputChannel = "broadcaster")
public String convert(String response) {
return response;
}
}
in the debbuger i'm getting this information:
30-03-2016 15:07:20 DEBUG SimpleBrokerMessageHandler:277 - Processing MESSAGE destination=/read session=null payload=3003
in another class (Controller) i'm using the same method:
this.messagingTemplate.convertAndSend(Constants.END_POINT_READ, "3500");
and sending "manually" the information an is showing correctly. and i'm getting in debbuger this message:
30-03-2016 15:05:18 DEBUG SimpleBrokerMessageHandler:277 - Processing MESSAGE destination=/read session=dfR45V77 payload=3500
the difference is in session value but i don't know what this session is having null in the process, what am i doing wrong some clarification o help is welcome
First of all I don't see the configureMessageBroker implementation, so it isn't clear how that may work at all...
From other hand if you see such a difference, try to debug the code in the SimpMessagingTemplate.
I only see headerAccessor.setSessionId(sessionId); in the SimpleBrokerMessageHandler when it does
logger.debug("Broadcasting to " + subscriptions.size() + " sessions.");
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