Jruby Rails on Torquebox calling EJB services - model classes - java

I'd like to play with Torquebox and have Rails (or e.g. Sinatra) as a frontend, but have all my services and business layer in EJB with CDI and JPA and all that stuff.
1) Can I have it all deployed as one application and use local calls for EJBs
2) If I have Rails, I can use models (no ActiveRecord) just to exchange data with frontend. How can I call EJB services with such models? I mean having:
def create
user = User.new(params)
userRepository = inject(Java::com.example.UserRepository)
userRepository.create(user) // this is java call
end
How can I pass User (jruby model to EJB call)? Or should I do
user = Java::com.example.User(params)
instead of plain ruby models.
Basically I'd like to have front in Ruby and the rest of machine in Java EE stack.

The TorqueBox integration tests have an app that does something similar, see:
https://github.com/torquebox/torquebox/tree/2x-dev/integration-tests/apps/rails3/twitter
It loads an ApplicationScoped bean from lib/ejb.jar, the source of which can be found at:
https://github.com/goldmann/confitura-2011-torquebox-demo/tree/master/cdi
Hopefully that will get you pointed in the right direction.

Related

Where should Business Delegate & Service Locator reside to not have circular dependency in this case?

I am working with Java 1.7, XDoclet 1.2.3, WildFly 8.2.1.Final, Dynamic Web Module 2.5, EJB 2.1 in Eclipse Luna.
I have an Enterprise Application project named
P001_EAR.
I have a Dynamic Web Project named P001_WAR.
I have a EJB Project named P001_EJB.
I have a EJB Client Project named P001_EJBClient.
I have a Utility Project named P001_SRC.
The P001_SRC contains the data layer, domain objects, business interface, helper classes.
The P001_EJB has Stateless Session EJBs which implements the business interface. It has a reference of P001_SRC.
The P001_EJBClient contains the remote and home interfaces of EJBs. It has a reference of P001_SRC.
The P001_WAR contains web stuff listeners, filters, servlets, JSPs, HTMLs. It has a reference of P001_SRC.
This is a typical scenario:
JSP call Servlet, Servlet call Business Delegate, Business Delegate call EJB (using ServiceLocator), EJB perform business operation.
Question is where to put Business Delegate?
I was thinking of putting them in P001_SRC but Business Delegate needs a reference to P001_EJBClient to perform their actions and that means a circular dependency.
How you would solve this issue?
Also the issue is to where to put the Service Locator? Would it go in the same project as Business Delegate?
Thanks
In a classic J2EE design pattern, the each client module of the EJB would have its own Business Delegate, which knows about the actual EJB. In your scenario, it would be in the WAR (as it's the client of the EJB). Similarly, your Service Locator would reside along with your Business Delegate and provide data about where your EJB is.
But seriously, is this some kind of exercise or school/interview question? Or are we traveling back in time? :-)
If this is for a new application, I would seriously consider moving away from EJB 2.x in favor of EJB 3.x, as it simplifies a lot. You can't even say that you "can't move", as you are already using an application server that supports EJB 3.x.

Java Spring: benefits of POJO objects

I am learning Spring using this tutorial. I am unable to get my head around the following excerpt from it:
Spring enables developers to develop enterprise-class applications using POJOs. The benefit of using only POJOs is that you do not need an EJB container product such as an application server but you have the option of using only a robust servlet container such as Tomcat or some commercial product.
In the good old days when application servers only supported EJB 2 it was a nightmare to develop services using EJBs. Each service (e.g. a stateless session bean) required a bunch of interfaces and strange additional methods to work properly (home interface, remote interface, deployment descriptors etc).
In order to run EJBs you need an application server such as Jboss or Glassfish. In order to run servlets you simply need a servlet container such as Tomcat or Jetty which is way more lightweight than an application server.
Spring offers a way of creating simple services as plain POJOs (that can be exposed via servlets). Therefore, to be able to develop services as a POJO was simply a dream come true. Services did not need all the constraining dependencies to the EJB-interfaces and they could be deployed in a lightweight servlet container.
Then came EJB3 which greatly improved life for the Java EE developer. EJBs no longer needed the dependencies for home- and remote-interfaces (at least not via inheritence). A modern EJB 3 service is very similar to a POJO-based service. The main difference is that EJBs still require an application server to be deployed.
Spring Guru Rod Johnson released the book J2EE Development without EJBs which greatly explains how to replace your old J2EE components (such as EJBs) with more lightweight Spring Pojos - good reading!
Read below link which may help you understand meaning of benefit of using POJO :
http://www.javaexperience.com/difference-between-pojo-javabean-ejb/

Inject a bean from another application context?

Is it possible to inject a bean from a web application that deploy in another server!
I declare a scenario to myself, I have two web application that use spring framework and deploy separately in different application servers (one is TOMCAT and another one is WEBLOGIC),the first application has ServiceA and the second one has ServiceB, now I want to inject ServiceB in ServieA?
I try to do this with RMI once an another one with JMS, now I am wondering that:
Is it possible with another thing?
Is there any active project about this scenario exist?
How can share application context in spring framework, is it possible?
Thanks.
Bean is just an object in JVM. You certainly cannot use an object from one JVM in another JVM straightforward.
But you can do 2 things:
Use proxies - some objects that will have the same interface but invoke somehow to the proper server as implementation.
Use service-oriented architecture (SOA). Each server should have some limited set of beans that are responsible for their functionality. And all beans can interact with each other.
Maybe OSGI is suitable for this.
Web services, JAX-RS is the simplest. But JAX-WS provides you with the tools to automatically generate the client code.

DI in an EJB/MDB Application

I'm currently developing a small EJB application running on IBM Websphere Application Server 7 (Java EE 5). The app mainly consists of one MDB listening for incoming MQ messages which are transformed and stored in a DB. Currently I'm using a lot of Singleton/Factories to share configurations, mappings, datasource lookups etc. But this actually leads to some very hard to test code. The solution might be using a (simple) DI framework like guice/spring to inject the different instances. The question is: Where to place the initialization/ setup code? Where is the main entry point of the application? How can I inject instances into the MDBs?
it might be worth looking at backing off from using Guice, and trying to work with the injection mechanisms already available with Java EE 5.
Regarding finding a suitable "startup point", unfortunately the EJB specification does not define a way where you can have a bean run at startup. However, the web profile of the EE spec does have one -- you can add a WAR to your application, and set a servlet listener component:
http://java.sun.com/javaee/5/docs/api/javax/servlet/ServletContextListener.html
You can set this to start whenever the application is loaded and started by the container (WebSphere). Watch out for classloader issues though.
Using Spring, you can do it via EJB3 interceptors, see http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/ejb.html#ejb-implementation-ejb3
Useful info on caveats are in the javadoc, make sure you read it: http://static.springsource.org/spring/docs/3.0.x/api/org/springframework/ejb/interceptor/SpringBeanAutowiringInterceptor.html

Calling web services from your JSF code

Let's say that you have a presentation tier in JSF, and that your business tier is accessed using web services. How would you call your web services from JSF?
I was considering to have my backing beans to call the web services, but I just though I could use Ajax with JSF in order to connect to the web services. What would you choose and why? Any other choice you could recommend?
EDIT: I'm using Spring in the business tier, maybe that info may help with the suggestions.
Thanks.
I'd wrap the web service call in a service class, that is accessed via the managed bean. Thus the front-end will not know how exactly the data comes to it - via web services, or via any other means.
Let's say that you have a presentation tier in JSF, and that your business tier is accessed using web services. How would you call your web services from JSF?
The "classic" approach would be to inject a JAX-WS proxy factory class (generated from the WSDL) in a ManagedBean:
public class ItemController {
#WebServiceRef(wsdlLocation = "http://localhost:8080/CatalogService/Catalog?wsdl")
private CatalogService service;
public DataModel getItems() {
if (model==null || index != firstItem){
model=getNextItems();
}
return this.model;
}
public DataModel getNextItems() {
Catalog port = service.getCatalogPort();
model = new ListDataModel(port.getItems( firstItem,batchSize));
return model;
}
}
Sample taken from Sample Application using JAX-WS, JSF, EJB 3.0, and Java.
I would implement EJBs and expose them as web service (for language independet remote access) within the application I would access the EJBs by lookup and direct call them (for better performance). Unfortunatly you did not tell what platform you're using, so I can't be sure whether my suggestions would be feasible.

Categories

Resources