sabato 22 dicembre 2012

Interfaces: Remote vs Local

Many of you may be confused as i was upon the terminology and the usage of interfaces.
Should they be remote? or should they be local?

IT DEPENDS.

Nice question, weird anser. Depends is always a good answer but I will explain here my point of view, after having read here and there some hints.

When you design your appliaction you first decide what it has to do, and doing so, you will decide the logic that your EJB will hold.
The separation between the "Logic"(hold by the EJB tier) and the "Presentation"(hold by the web application, or generally your client) following the EJB architecture is foundamental, and here it comes the time to decide what you want your client to be.

i.e. Suppose you want your logic to implement a calculator, you will provide sum, subtraction, multiplication and division through EJB.

The interfaces are just the view exposed to the client of your Logic, without knowing of course how it is implemented.

i.e. your client will provide a sort of gui or some input methods for the numbers and the operation you want to do, hence calling the EJB it will perform the computation.

At this point you may wonder where you should have your client.

At the very beginning EJB was meant to provide remote access throught RMI standard, so the client was meant to be on a different machine from the one who acted as container for EJBs (remote).
Moving to version 2.0 the Java EE team decided to provide also a different approch to EJB. You wonder: why should i run through RMI, sockets and network connection when i have the client on the same JVM as the EJB container?
So they introduced the local interface that allows the client to call directly methods on bean, bypassing the RMI sematics(local), you will find that you write local interfaces in some special cases.

How much good is this idea to have it local?? I can't say. Still you should have a stub and a skeleton as the RMI paradigm impose, but you don't have the network in the middle because the client and the server(ejb container) run on the same java virtual machine and hence on the same heap.
Instead with the remote interface you have two different JVM and two different heaps.

Anyhow, you have to choose based on your client-program. For sure you will not have both the implementation, it will be useless. Think about it, should you have a remote interface and a local interface that expose the same things? or partial things on the remote and partial on local?

Furthermore in my opinion i think that if you are going to use EJB to provide the logics, your client will be running on a different machine.



other sources:
http://stackoverflow.com/questions/3807662/ejbs-when-to-use-remote-and-or-local-interfaces
http://stackoverflow.com/questions/1385717/why-do-we-need-separate-remote-and-local-interfaces-for-ejb-3-0-session-beans

book: Head first EJB

venerdì 21 dicembre 2012

Installing and configuring JBoss

As you may have noticed if you installed Netbeans (the full version) as IDE, you have also installed the latest version of tomcat and the latest version of glassfish.
For many of you thoose servers may be enought to start writing code, and testing the available technologies.

With this post we will explain you how to install a new server: JBoss AS 7, that for many of you may be already a know resource, but for other may be a new one.

This server that will be installed is Java EE compliant, as glassfish is (remember that Tomcat is not), and is provided by JBoss (http://www.jboss.org/).

So let's start.


1) Install latest version of JBoss AS (7 currently, at the time of the post writing).

Instruction for doing so can be found here for those who use Windows OS
https://docs.jboss.org/author/display/AS7/Installing+and+starting+JBoss+AS+on+Windows

for the others who use Linux based systems or MacOS

https://docs.jboss.org/author/display/AS7/Installing+and+starting+JBoss+AS+on+Linux,+Unix+or+Mac+OS+X


We installed JBoss on windows 7, Mac Os 10.6 and Ubuntu 10.10 and the lastest Linux Mint (at writing time), it is working perfectly without any problem.

RECAP OF INSTALLING: unzip and choose a folder where to place it. That place will be - inside the jboss 7.1.x - the home for the server.

At this point you have JBoss AS 7 installed.

2) Configure IDE

For the sake of semplicity we'll use Eclipse for which there are the latest updates available coming directly from the jboss comunity (i have to say that personally i like more NetBeans - that atm does not support JBAS7).
Once you have eclipse installed follow the steps described here

https://www.jboss.org/tools/download/installation/update_4_0
(Section is "Installation from update site")

that said, continue with this steps to set up and use Jboss with eclipse

https://docs.jboss.org/author/display/AS7/Starting+JBoss+AS+from+Eclipse+with+JBoss+Tools

once you have finished this step your job is done.
You have successfully configured eclipse to use your jboss server.

Enjoy your server!

Ps: Note that the server provided in this way is suitable only for testing and building application to run on your localhost, for configuring your server in order to accept connection not only from localhost please check out this post http://javafortheweb.blogspot.it/2012/12/accessing-remote-ejb-on-jboss-as-71.html

EDIT: update on 10th april 2013. Tested on Mac OS 10.8.3. Working successfully!

Accessing remote EJB on JBoss AS 7.1 from web application

Finally, after many and many hours spent on a solution, I'm able to deploy an EJB module and access it from a differente machine! Here are the steps:

Environment: 
1) JBoss AS 7.1 running on a Debian Distribution. To build and deploy the EJB module I used Eclipse (juno version)
2) Apache Tomcat 7.0.32 on a DIFFERENT machine on which I'm going to develop a simple servlet to get the remote ejb deployed on JBoss. I used Netbeans 7.2 here, just because I still prefer Netbeans when developing web applications. 


STEP 1. This is the key point of the entire work. I found hundreds of guides on the web explaining how to invoke remote ejb from a separated client, but noone of these pointed out this important issue. Either you are using Glassfish or JBoss to develop your EJB module you first have to set your server in such a way that is reachable from the outside and not only in localhost. To do this you have to edit the server descriptor, in this case ( JBoss ) standalone.xml. In ${JBOSS_HOME}/standalone/configuration/ there is our file, open it. Quite at the end of the file, we can find the interfaces definitions; ADD A NEW INTERFACE (I called it mine), setting the access of it to any ip. The result should be this:
  
        
            
        
        
            
        
        
            
        
        
            
        

    
Following the interfaces, we can find the socket-binding-group tag, which sets the ports required for the given interfaces. In our case, change the default-interface parameter to match the new interface ( mine again in this case ); the outpout should be:
 
        
        
        
        
        
        
        
        
        
        
        
            
        
    
STEP 2. Now we need to add a new APPLICATION user to the JBoss server. To do that run the add-user script ( ${JBOSS_HOME}/bin/ ) and select the b) option. The user and password will be used by the client to access the EJB module on the server. The a) option instead (managment user) allow you to create a username and password needed to access the administration panel of JBoss.

STEP 3. We are ready to develop an EJBModule on our JBoss server. Just create for example a stateless session bean on eclipse, providing a remote interface:

session bean pattern:

package ejb;

import javax.ejb.Remote;

@Stateless
@Remote(RemoteFirstBean.class)
public class FirstBean implements RemoteFirstBean{
...
//implemtation of interface's methods
}

interface pattern:

package ejb;

public interface RemoteFirstBean{
//declaration of methods
}

Now we can deploy the module on JBoss, no errors should rise up.

STEP 4. On my client pc, I create a java Web application using Tomcat as web server. After that, just create a servlet including in the processRequest method the following code just to test our remote invocation:

final Hashtable jndiProperties = new Hashtable();
jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
try {
            final Context context = new InitialContext(jndiProperties);
            String appName = "";
            String moduleName = "jboss-firstbean";
            String distinctName = "";
            String beanName = "FirstBean";
            String interfaceFullName = "ejb.RemoteFirstBean";
            final String jndi = "ejb:" + appName + "/" + moduleName + "/" + distinctName + "/" + beanName + "!" + viewClassName;
            RemoteFirstBean statelessRemote = (RemoteFirstBean)context.lookup(jndi);

        } catch (NamingException ex) {
            System.out.println("problems");
        }

As we can see, first create an InitialContext putting "org.jboss.ejb.client.naming" as url_pkg_prefixes. This tells our application to lookup in his classpath for the jboss-ejb-client.properties file in which it will find all the information to access our remote ejb. Now we just need to lookup in the context for the bean using the jndi specification used the JBoss (remember to include in the project the same interface as the JBoss one). The jndi specification is the following:

ejb:{app-Name}/{module-Name}/{distinct-Name}/{bean-Name}!{fullPath-remote-Interface}

Appname should be the name of the .ear file we have deployed on JBoss (if deploying an Enterprise Application) . In my case I just deployed a .jar file as simple EJB module so app-name will be empty. Module-name is the name of the EJB module deployed ( the .jar file on JBoss ), in my case is "jboss-firstbean". Distinct name as well is empty. The full path of the remote interface in my case is ejb.RemoteFirstBean. The resulting jndi will be:

ejb:/jboss-firstbean//FirstBean!ejb.RemoteFirstBean

After the lookup, we can try some methods of the bean to verify if everything works.

STEP 5. As I said before, we must now set the properties file. It must be placed in the classpath of the web application. Create a file with the name jboss-ejb-client.properties and insert the following(field,value):

remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false
remote.connections=default
remote.connection.default.host=192.168.1.108
remote.connection.default.port=4447
remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false
remote.connection.default.username=USERNAMEUSEDINSTEP2
remote.connection.default.password=PASSWORDUSEDINSTEP2

4447 is the default port for remoting of JBoss. Just use the Ip of the JBoss server machine.

STEP 6. Last step. In order to use org.jboss.ejb.client.naming on the client application, I needed to include in my project the jboss-client.jar library which can be found in ${JBOSS_HOME}/bin/client/

STEP 7. Run the servlet, and all should works fine :) (EJBModule should be running as well :P )

Integrated Development Environment

There are very many tools that you may want to use for programming in your favourite language.
I will suggest some, that i found really very much helpful.

Since we're going to program with Java, the main IDEs available on line are in my opinion:
Icon di netbeans
  • Netbeans
    current version (at the date of writing) 7.2 with a 7.3 beta version available
    You can find Netbeans here:
    http://netbeans.org/



Icon di Eclipse




Both runs on Mac OS systems, Windows and linux based OS (mostly Debian distro derived like Ubuntu, Kubuntu etc etc)

Personally i like more Netbeans, don't ask me why because it would take too long to be explained.
For the purpose of programming for the web, testing or learning a new technology i suggest Netbeans in its full version (which supports also other programming languages like c/c++..).
Furthermore Netbeans come along with:
  • Glassfish server version 3.1.2 (may vary)
  • Apache Tomcat 7.0.27 (may vary)
Anyhow there will be some things that, for the sake of semplicity, we will do using Eclipse!
For the purpose of showing how does some technology works we will explain in another post how to install and configure JBoss which is another server Java EE compliant as Glassfish is.

Theese are the tools we're going to use. You will choose the Environment that you like more.
I think that an IDE helps a lot for programming, and simplify the life of programmers but you can still write code using:
  • gedit
  • notepad++
for example and feel to compile a part and run programs through the shell as well.

Who are we?

I am Lorenzo Massimo Gramola, i will write this blog in collaboration with Massimiliano Battan.

Lorenzo Massimo Gramola
I have a bachelor degree in Computer Science, i actually study for the Master degree in Italy at the University of Trento.

I like very much Java and its environment.
I will keep this blog alive with new posts whenever possible ;)

Massimiliano Battan
I have a bachelor degree in Computer Science too and I'm studying for the Master degree in Italy at the University of Trento.