Calling EJB from war client - ejb

I have my application ear .Now inside this ear I have two parts .
First EJB module
Second Web Module packaged inside the war. So my Ear root looks like
EAR
---EJB Modules
---WAR Modules
Now I have a requirement to call one of the ejb from one of the war client. Is there any way other than remote EJB call to do the same.

#Named
#SessionScoped
public class WarManagedBean{
#EJB
private EjbBean ejbBean;
public void method(){
//Using of EJB
}
}

Related

Global module with ejbs

I deployed a global module with remote ejbs defined, but I can't access them from deployed applications.
Here is the interface:
#Remote
public interface ICryptoAPI..
and bean definition:
#Stateless
public class CryptoAPI implements ICryptoAPI ...
How can I find jndi name of the ejbs deployed, since default #Ejb cannot find bean? Just to mention, /subsystem=naming:jndi-view() from jboss-cli doesn't print anything about deployed module and there are no errors in wildfly log.
Maybe, it is not possible to define injectable ejbs in global modules?
The portable JNDI name, an EJB is bound to, depends on how beans are deployed and some configurations (e.g. deployment descriptors).
In addition to the portable JNDI name, JBoss provides access from a remote VM within the java:jboss/exported namespace.
For your convenience, JBoss' default logging shows the JNDI names, an EJB is bound to, on console and in the server logfile upon deployment. The logger is org.jboss.as.ejb3.deployment on INFO level.
Example:
12:00:00,000 INFO [org.jboss.as.ejb3.deployment] (MSC service thread 1-6) WFLYEJB0473: JNDI bindings for session bean named 'MyBean' in deployment unit 'subdeployment "my-beans.jar" of deployment "my-app.ear"' are as follows:
java:global/my-app/my-beans/MyBean!my.package.MyBeanRemote
java:app/my-beans/MyBean!my.package.MyBeanRemote
java:module/MyBean!my.package.MyBeanRemote
java:jboss/exported/my-app/my-beans/MyBean!my.package.MyBeanRemote
For accesing the EJB, you can do a manual JNDI lookup or use the #EJB annotations lookup method for injection.

How can I execute a method by accessing autowired object in spring MVC from separate standalone java application

Let's we have a class in webapp.war (spring MVC 4.2.2.RELEASE)
public class SomeClass{
#Autowired
private MyInterface implObject;
public void method1(){
implObject.doSomething();
// statements ...
}
}
and another class in standalone.jar
public class MainClass{
public static void main(String[] args){
// want to create object of SomeClass
// or execute doSomething() ...
}
}
Note: application will be deployed in clustered environment, standalone.jar will be executed by shell script (it will be registered in crontab).
I want to schedule some job (fetch records and send to weblogic queue JMS ...), using Unix crontab. and don't want to repeat DB operation separately (in standalone.jar).
Please also suggest if I can make standalone.jar small in size.
Quartz or similar implementation is not expected in my case.
Thank you.
I solved this problem by extracting necessary part of webapp.war, into app.jar.
Scheduling and batch configured using crontab.
Used maven build tool i.e maven-shade.
Any required configuration left to spring.

Can I add a JNDI Binding with a customized name for inject a Stateless Bean on my client Servlet?

In one hand, I have a Stateless EJB Bean which implements a remote interface.
#Stateless(name = "ejbBean")
#Remote(TimedBean.class)
public class TimedBeanImpl implements TimedBean
...
On the other hand, I have a Servlet client where I need to inject this EJB bean for invoking its operations. The injection referts to a JNDI automatically generated by the server:
public class LoadTimer implements ServletContextListener {
// EAR Local Mapping - EAR Dev Mapping
// #EJB(mappedName = "java:global/appTest/appTestModuleOne-01.00.00/ejbBean")
#EJB(mappedName = "java:global/appTest-01.00.00/appTestModuleOne-01.00.00/ejbBean")
private TimedBean timedBean;
#Override
public void contextInitialized(ServletContextEvent servletcontextevent) {
...
It works.
But the fact of referencing the jndi automatically generated makes the solution highly dependent on the specific environment:
It depends on the server. For example, my local server for testing it's a JBOSS 7.1 and my dev server it's a JBOSS EAP 6.2, and the automatically generated jndi's are different.
It depends on the packaging. Not is the same refers to the ejbBean deploying in a EAR file than in a WAR file.
And it depends on the version of artifacts. More changes.
I want, if possible, is to specify a unique name for the bean can be referred to the servlet in each server, for each version and packaging, in order not to have to be making continuous changes in development and avoid errors in deployments.
Greetings!
You can use <application-name> in application.xml and <module-name> in your module descriptor (web.xml or ejb-jar.xml) to mitigate the hardcoded version numbers. (Note that as of EE 6, you should use #EJB(lookup="...") rather mappedName.) The spec also requires that your application server give you an option to override the lookup name of the #EJB reference while deploying the application, so it should not matter what is hardcoded in your application at development time.

Spring Integration: JDBC single query to web service

I would like to know the way for resolving this integration scenario:
Execute different queries to select X elements from a database. I am
looking for an inbound adapter without pooling because it is just
necessary to execute the query once. Although, results of the queries
will be generate only one output.
Work with this data to build a SOAP request (generic web service)
Send this SOAP request to a web service and wait for an asynchronous response.
But also, it is necessary to deploy all this scenario in a WAR file on Tomcat server. I am deploying the application from a spring MVC + spring integration skeleton but I will not have any controller. Is it possible to execute the application when context was loaded on Tomcat?
I am working with the next technologies:
Spring integration
Spring MVC for a WAR deployment
Scheduling (Quartz or #Scheduled)
Spring WS
Regards
Since you say that you'd prefer to select on the application start up and only once, you can use:
<int-event:inbound-channel-adapter channel="jdbcChannel"
event-types="org.springframework.context.event.ContextRefreshedEvent"
payload-expression="''"/>
and <int-jdbc:outbound-gateway query="SELECT * FROM ..."/>
And so on to the WebService.
UPDATE
Since you say that you are around Anotation configuration, consider to use Spring Integration Java DSL.
To configure <int-event:inbound-channel-adapter> from #Configuration you should do this:
#Bean
#SuppressWarnings("unchecked")
public MessageProducer ApplicationEventListeningMessageProducer() {
ApplicationEventListeningMessageProducer producer = new ApplicationEventListeningMessageProducer();
producer.setEventTypes(ContextRefreshedEvent.class);
producer.setPayloadExpression("''");
producer.setOutputChannel(jdbcChannel());
return producer;
}
ContextRefreshedEvent info you can get from its JavaDocs or from Spring Framework Manual.

Integration testing for a single service mocking/stubbing some of the injected components

I've read hundred of posts and pages but I'm unable to figure the right way to do integration testing mocking just some components.
This is the scenario: I've an application created using Spring Boot (1.2-snapshot) and among various spring libraries, also spring data JPA.
I've several services, for example Service1 and Service2, and they use other components and repositories managed by Spring Data.
If I want to test all the services for a complete integration testing using an embedded hsql database I declare a class this way in my test package:
#Transactional
#RunWith(SpringJUnit4ClassRunner.class)
#SpringApplicationConfiguration(classes = AppConfig.class)
public class IntegrationTest {
#Autowired
private Service1 s1;
#Autowired
private Service2 s2;
[... test methods ...]
}
Where the AppConfig class is instead in my main package, and is defined this way:
#ComponentScan
#Configuration
#EnableAutoConfiguration
public class AppConfig {
public static void main(String[] args) {
SpringApplication.run(AppConfig.class, args);
}
}
During the integration testing a complete spring context get defined, all the spring data repositories are built and instantiated as usuale and everything works fine as expected.
But there 2 are scenarios where I need different goals:
1) I want to test just one service at a time (e.g.: Service1), for example because Service2 is very slow to initialize, and I want to test it in a different test class.
How do I achieve this goal? The problem is that I still need all the Dependency Injection, and in particular all the spring data manages repositories that Service1 autowires on itself. If I was not using spring-data repositories I could new() the Service1 class by myself and then wire all the dependencies by hand, even this would be very cumbersome.
2) While testing Service1, I would like do mock/stub just one of all his dependencies. For example I would simulate a component that in production connects to external services.
I don't know how to selectively inject a stubbed object on the spring context while continuing to use all the others as usual.
Some help on the subject would be very welcome.
Instead of using Mockito mocks you can create different test beans that are set up on different profiles and then execute the tests with those profiles.
FOr example (of course it's just a vision of how you can do it not the exact solution) you could add a profile 'fast' and run your tests with #ActiveProfiles('fast'). Then you would have test configurations annotated with #Profile('fast') that would set up all the beans that you need

Resources