EJB Spec says you shouldn't manage threads. I have seen Bean code that sends remote requests and loops with a Thread.sleep waiting for a response to reduce CPU usage. From what I understand this breaks spec. Does simply calling the logic from a separate POJO or library that is instantiated then referenced in the EJB's method fix this? Does simply removing Thread.sleep fix the issue at the cost of additional CPU consumption? How should external synchronous requests be coded in EJBs?
1
There are 1 answers
Related Questions in JAVA
- I need the BIRT.war that is compatible with Java 17 and Tomcat 10
- Creating global Class holder
- No method found for class java.lang.String in Kafka
- Issue edit a jtable with a pictures
- getting error when trying to launch kotlin jar file that use supabase "java.lang.NoClassDefFoundError"
- Does the && (logical AND) operator have a higher precedence than || (logical OR) operator in Java?
- Mixed color rendering in a JTable
- HTTPS configuration in Spring Boot, server returning timeout
- How to use Layout to create textfields which dont increase in size?
- Function for making the code wait in javafx
- How to create beans of the same class for multiple template parameters in Spring
- How could you print a specific String from an array with the values of an array from a double array on the same line, using iteration to print all?
- org.telegram.telegrambots.meta.exceptions.TelegramApiException: Bot token and username can't be empty
- Accessing Secret Variables in Classic Pipelines through Java app in Azure DevOps
- Postgres && statement Error in Mybatis Mapper?
Related Questions in JAKARTA-EE
- How can I calculate the number of matches of a jakarta.ejb.ScheduleExpression within a time interval in Java?
- How to connect to cloud sql when using app engine instance in java 21 runtime?
- Glassfish 7.0.12 adds question mark to URL when running JAVA EE Application
- Weblogic: The Message Driven Beans in the war file are not reflecting in Weblogic 14.1.1
- Jakarta EE 10 serializing person entities results in recursion checker exception (from org.eclipse.yasson.internal.serializer.RecursionChecker)
- EJB transactions behaving differently on Wildfly 8 between Windows and Linux deployments
- Is EntityManager injected with @PersistenceContext to a @RequestScoped CDI bean thread-safe?
- Redirect user based on his Role in Jakarta EE web app
- CXF web service deployed with docker compose won't work
- Messages won't reach the JMS backend in Weblogic JMS (BEA Server)
- Using XML as config-property value
- JEE-Transaction- vs. JPA Entity Management
- Jakarta CDI force bean construction/register legacy event listeners
- WildFly localhost 'forbidden' access
- WSSTUBE0025: Error in Verifying Security in the Inbound Message (Security Requirements not met - No Security header in message)
Related Questions in EJB-3.2
- Wildfly30 - authentication between WAR and EJB
- Usage of Singleton EJB with CMC and CMT to avoid Hibernate's "Row was updated or deleted by another transaction"
- EJB calls over HTTP | client Authentication issue
- Is it important to delete deleted from code timers from jboss_ejb_timer. EJB3. POSTGRES
- Cannot implement Java remote interface to communicate with EJB application running on Websphere Liberty
- HTTP Request from EJB Bean
- Not rollback in multi-tenancy with hibernate and JakartaEE
- Does adding @TransactionAttribute(REQUIRED_NEW) in a method which is invoked from a Stateless Bean(which already has REQUIRED) work in EJB 3.1?
- Injection causes WELD-001408: Unsatisfied dependencies
- Lookup Remote EJBs on Liberty (wlp-javaee8.21.0.0.8)
- Wildfly JavaEE 8 - Double initialization of Singleton EJB
- Error finding remote EJB with Spring Boot war app on Wildfly
- Understanding EJB Architecture and Implementation
- Are Optional ElementCollections Possible In Hibernate
- Assuming a CLI or Swing interface client, how is a remote Bean accessed through Liberty
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
Popular Tags
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
That depends on the business case. EJB spec provides plenty of resources for async/sync processing without boilerplate code using
Thread,Runnableor any other mechanism.To execute a piece or code asynchronously (that is, the caller won't wait for the response, but carry on), use
@Asynchronous, andFuture<T>if you want to listen for responses afterwords.A synchronous call, as you called, is a call that waits for the response, so "How should external synchronous requests be coded in EJBs" is something that doesn't need any kind of asynchronous/background execution. You just make the call and the code itself wait for the response (otherwise it would be asynchronous), being the tipical case a Web Service (either REST or SOAP).
Web Services calls can actually be synchronous or asynchronous, that depends on the business case, but they are usualy synchronous, you make the call and receive a response with the data. In cases of business logic that takes a while to execute, the Web Service receives the resquest and may launch the business logic asynchronously (with an
@Asynchronousfor instance) and respond immediately with a plain HTTP202 - Accepted, which basically means "Hey! The request you just sent me is gonna take a while, so I'll do it in the backround".In that case, may be you have another web service that you need to check to see how that long lasting process is going. That is the only case I can think of in which someone will want that
Thread.sleep(...)in a loop, checking the Web Service until it tells you that the process have finished.Luckily, EJB also provides a solution for that business case:
@Schedulemethods in case you need to check/do something indefenately, in specific intervals: something to do every day at 02:00, or every first day of month, or even every 2 seconds.TimerServiceand@Timeout, in case you want to programatically schedule a single task. This last fits better in the business case we are talking.So you call the
TimerServicewith the timespan you want to wait for the next check. When time comes the@Timeoutmethod is fired, in which you can check whatever you need, and shcedule another execution in case you need it, even with a new timespan.