EJBs for Everyone: Consume EJBs in .NET Using C# and a Web Service

EJBs for Everyone: Consume EJBs in .NET Using C# and a Web Service

he complexity involved in building enterprise-class apps is, unfortunately, beyond the skill-set of your average developer. It requires the consideration of a lot more than your actual application logic. The framework on which your application resides needs to handle high traffic, availability, and performance?all before you even write the specifics of your application! Done correctly, the framework takes care of the broad requirements of performance and scalability, leaving your developers free to write applications that are concerned only with their application-specific logic.

Architects generally face a fork in the road in the planning stages when designing an enterprise system. Should you build all of it yourselves? Going down to the level of threading models, messaging queues, object pools, and database connection pools? Or should you use an off-the-shelf enterprise application framework? This is where the J2EE spec, and more importantly the application server that implements this spec, comes in handy. If you choose to use an application server such as WebSphere, Weblogic, or JBoss, the infrastructure is already built for you. More importantly, it is tested and proven in many environments. In many cases, this is clearly a better option than trying to grow your own and test, deploy, and maintain it.

It seems like a no-brainer. Except that if you choose to use a pre-existing infrastucture, your apps need to be able to work with it.

EJBs to the Rescue
If your critical business logic resides in a technology like EJBs, your app will be infrastructure-ready. Once again, this not an easy task. Most companies have a large pool of Visual Basic, Java, or C# developers who are simply not up to this challenge. But Visual MainWin changes this, allowing you, the Visual Basic, Java, or C# developer to build an application (using C# or VB.NET) you can deploy to a J2EE application server.

Writing EJBs is but one part of this task?you also have to consume them. Of course, consuming them can be just as difficult to the uninitiated. A well-designed application will have your EJBs in a business tier, which hopefully should not change too frequently. A high-level view of a typical architecture would look something like Figure 1.

Figure 1. Here’s a high-level view of a typical architecture.

An Enterprise Information System (EIS) may be scattered across many databases or other services, which are integrated using an abstraction layer termed the integration tier. The business tier takes this data and adds value to it through business logic. For example, an EIS may contain a real-time data source indicating the price of a stock, and another less-frequently updated data source containing the last posted earnings for a company. An EJB, residing in the business tier then calculates the common P/E (Price over Earnings) analytic. The presentation tier consumes this EJB in a given session and renders the results to the user.

In many cases, the rate of change of an application decreases the further you move away from the user. For example, in the above case P/E is always P/E and the EIS does not frequently change. As clients use your application, requirements change, and as such there is the need for changes such as in the presentation of the application or the addition of new features or integration points.

All too often, enterprises get stuck here. They discover the productivity benefits of using Visual Studio.NET and want to use it. However, plugging .NET into EJBs is not an easy task. They can bring in expensive J2EE experts to wrap the EJBs as Web services for .NET to consume and hope not to hit interoperability errors. Or they can throw away the EJBs and start again, building from the ground up using Microsoft tools and services.

Neither option is particularly attractive, and Mainsoft have brought a new option to the table with the introduction of Visual MainWin. The application is reviewed here, and an article in how to develop a simple Web service is posted here. The rest of this article will step you through creating a new presentation layer application as a Microsoft Web Forms application that consumes an EJB.

Editor’s Note: Laurence Moroney is currently the Director of Product Evangelism at Mainsoft; however, he wrote this article for DevX before joining Mainsoft.

Construct a Sample EJB with Weblogic Workshop
If you don’t have an EJB handy to play with, this section will show you how to develop one using Weblogic Workshop, for deployment on Weblogic 8.1. BEA has done a magnificent job of making this part easy for you, so if you don’t have it already, it is well worth a download.

Once you have installed Workshop, run it and start the application server.

  • Click ‘Create’ and the EJB will be created for you (see Figure 2).
  • On the EJB visualization in the center of the screen, right click and select ‘Add Component Method.’
  • A method called void method1() will be created.
  • Click the link to this method to enter the code editor.
  • Change method1() to the code below:
    /** * @ejbgen:remote-method */public double getPE(String strTicker){  double dReturn = 0.0;  if(strTicker=="IBM")      dReturn = 2.2;  else      if(strTicker=="MSFT")          dReturn = 12.1;      else          dReturn = 1.0;  return dReturn;}
Figure 2. Click ‘Create’ and the EJB will be created for you.

The above code simply generates a value based on a string that is passed in. A real PE calculator would connect to two back-end data sources, or a single integration tier, get the values and divide them to get the real P/E. For the purposes of this demonstration, simple dummy data is used instead.

From the Build Menu, select ‘Build Application.’ The EJB will be built along with all the relevant project files and deployment descriptors, and deployed to the application server.

Consuming the EJB with C#
If you haven’t already gotten your hands on a copy of Visual MainWin, click here to get a 30-day evaluation copy. Once you have installed it, selecting Weblogic as the target application server in the process, start Visual Studio and select a ‘New ASP.NET Web Application’ from the ‘Visual MainWin C# for J2EE projects’ on the ‘New Project’ dialog. In the location name textbox, change ‘WebApplication1’ to FinancialAnalytics.

You now have a standard Microsoft Web Forms application called Financial Analytics. To make this application consume the EJB, take the following steps

  • In the Solution explorer, select the ‘References’ folder.
  • Right Click it and select ‘Add EJB Reference.’
  • The ‘Add EJB Reference’ dialog will appear. At the top of it, select the caret (…) button and browse to the JAR file containing your EJB. This should be in your {beahome}user_projects Applications Analytics directory.
  • Select the .jar file and click OK.
Figure 3. The Visual Studio ‘Add EJB Reference’ dialog shows the EJB.

The dialog should update to look like Figure 3, showing you the JNDI name of the EJB.

Click OK on this dialog and the EJB reference and C# proxies will be created for you.

If you encounter any errors on this dialog, make sure that you have the latest version of the Visual J# redistributable file installed.

In your solution explorer, you will see a new folder, called ‘EJB References’ and an entry within it simply called localhost. If you are familiar with developing using Visual Studio, it will look very familiar as it is the same development paradigm that is used to consume Web services via their WSDL.

Use this reference to declare your EJB proxies within C#. To consume the EJB from the earlier you would use code like this:

localhost.GetPERemote myPE = new localhost.GetPERemote();double d = myPE.getPE(txtTicker.Text.Trim());

The declaration to the EJB remote interface is declared in much the same way that you would declare an object to consume a Web service. The object in this case is myPE, which is an instance of the EJB proxy that MainWin creates for you. To call the EJB functionality, you simply call the relevant method on the proxy object, which in the above case is getPE.

Consume an EJB on a .NET Server
Some of you may have noticed a small problem. The C# code that consumes the EJB has to run on the J2EE application server! This seems to be a small disjoin, as you may prefer to have clean C# code running on a .NET server in a services or presentation tier consuming your EJB code running in a business logic tier, but you are tied to needing J2EE in the services and presentation tier too, which may break your design goals.

Here’s one potential solution to this problem: Visual Studio allows you to add projects to a project. So, if the machine has both IIS and the J2EE application server, such as Weblogic 8.1 running on it, you can create a solution that has a Visual MainWin for C# project in it, exposing a public class that consumes the EJB, and a standard C# project that consumes this public class. The standard project runs on .NET and consumes the class directly. Unfortunately, this doesn’t work (although it compiles and runs cleanly) because the application on the .NET server doesn’t have the J2EE references, and as such crashes at runtime.

You want a logical separation of tiers, with the presentation tier running .NET and the business logic tier running J2EE. You also want the simplicity and ease of use with C# in the business logic tier.

Here is the best solution, and it’s the obvious one: Web services.

In the above case, you built a Web application running on the same application server as your EJB that consumed the EJB. If you want a logical separation of the tiers as shown in Figure 1, with the presentation tier running .NET and the business logic tier running J2EE, but want the simplicity and ease of use with C# in the business logic tier, then you would build a C# Web service using Visual MainWin, and have that consume the EJB, exposing it’s methods through a Web Services Webmethod. Your presentation tier component then simply consumes that Web service using SOAP.

To build a Web service using Visual MainWin for C#, create a new application and from the new application dialog box select ‘New ASP.Net Web Service’ from within the Visual MainWin for C# folder. This creates the common Microsoft ‘Hello World’ Web service. Add a reference to the EJB in exactly the same way as you did earlier in this article and then add the Webmethod shown below.

[WebMethod]public double getPE(string strTicker){   localhost.GetPERemote myProxy = new localhost.GetPERemote();   return myProxy.getPE(strTicker);}

Running the Web Service under Visual MainWin isn’t very interesting as you don’t get the test harness that .NET gives you, but it does give you the path to the WSDL, which is ultimately what you are interested in.

On your presentation tier, you can now create a standard Microsoft.NET application, Web application, or Web service that consumes this Web service by pointing it at this WSDL to create a Web reference in the standard way.

The download with this article includes the Weblogic Java code for the EJB as well as a .NET project that has a Web application consuming the EJB as well as this Web service and a simple client that consumes it.

So that’s how you can use Visual MainWin to consume EJBs in your business logic tier. If necessary, you can further expose them to other tiers using Web services interop or through straightforward EJB references if the other tiers also run J2EE. The capability to consume EJBs now lies in the hands of your VB.NET and C# developers, freeing up your J2EE architects to concentrate on what they do best!


About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist