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!

devx-admin

devx-admin

Share the Post:
Software Development

Top Software Development Companies

Looking for the best in software development? Our list of Top Software Development Companies is your gateway to finding the right tech partner. Dive in

India Web Development

Top Web Development Companies in India

In the digital race, the right web development partner is your winning edge. Dive into our curated list of top web development companies in India,

USA Web Development

Top Web Development Companies in USA

Looking for the best web development companies in the USA? We’ve got you covered! Check out our top 10 picks to find the right partner

Clean Energy Adoption

Inside Michigan’s Clean Energy Revolution

Democratic state legislators in Michigan continue to discuss and debate clean energy legislation in the hopes of establishing a comprehensive clean energy strategy for the

Chips Act Revolution

European Chips Act: What is it?

In response to the intensifying worldwide technology competition, Europe has unveiled the long-awaited European Chips Act. This daring legislative proposal aims to fortify Europe’s semiconductor

Revolutionized Low-Code

You Should Use Low-Code Platforms for Apps

As the demand for rapid software development increases, low-code platforms have emerged as a popular choice among developers for their ability to build applications with

Software Development

Top Software Development Companies

Looking for the best in software development? Our list of Top Software Development Companies is your gateway to finding the right tech partner. Dive in and explore the leaders in

India Web Development

Top Web Development Companies in India

In the digital race, the right web development partner is your winning edge. Dive into our curated list of top web development companies in India, and kickstart your journey to

USA Web Development

Top Web Development Companies in USA

Looking for the best web development companies in the USA? We’ve got you covered! Check out our top 10 picks to find the right partner for your online project. Your

Clean Energy Adoption

Inside Michigan’s Clean Energy Revolution

Democratic state legislators in Michigan continue to discuss and debate clean energy legislation in the hopes of establishing a comprehensive clean energy strategy for the state. A Senate committee meeting

Chips Act Revolution

European Chips Act: What is it?

In response to the intensifying worldwide technology competition, Europe has unveiled the long-awaited European Chips Act. This daring legislative proposal aims to fortify Europe’s semiconductor supply chain and enhance its

Revolutionized Low-Code

You Should Use Low-Code Platforms for Apps

As the demand for rapid software development increases, low-code platforms have emerged as a popular choice among developers for their ability to build applications with minimal coding. These platforms not

Cybersecurity Strategy

Five Powerful Strategies to Bolster Your Cybersecurity

In today’s increasingly digital landscape, businesses of all sizes must prioritize cyber security measures to defend against potential dangers. Cyber security professionals suggest five simple technological strategies to help companies

Global Layoffs

Tech Layoffs Are Getting Worse Globally

Since the start of 2023, the global technology sector has experienced a significant rise in layoffs, with over 236,000 workers being let go by 1,019 tech firms, as per data

Huawei Electric Dazzle

Huawei Dazzles with Electric Vehicles and Wireless Earbuds

During a prominent unveiling event, Huawei, the Chinese telecommunications powerhouse, kept quiet about its enigmatic new 5G phone and alleged cutting-edge chip development. Instead, Huawei astounded the audience by presenting

Cybersecurity Banking Revolution

Digital Banking Needs Cybersecurity

The banking, financial, and insurance (BFSI) sectors are pioneers in digital transformation, using web applications and application programming interfaces (APIs) to provide seamless services to customers around the world. Rising

FinTech Leadership

Terry Clune’s Fintech Empire

Over the past 30 years, Terry Clune has built a remarkable business empire, with CluneTech at the helm. The CEO and Founder has successfully created eight fintech firms, attracting renowned

The Role Of AI Within A Web Design Agency?

In the digital age, the role of Artificial Intelligence (AI) in web design is rapidly evolving, transitioning from a futuristic concept to practical tools used in design, coding, content writing

Generative AI Revolution

Is Generative AI the Next Internet?

The increasing demand for Generative AI models has led to a surge in its adoption across diverse sectors, with healthcare, automotive, and financial services being among the top beneficiaries. These

Microsoft Laptop

The New Surface Laptop Studio 2 Is Nuts

The Surface Laptop Studio 2 is a dynamic and robust all-in-one laptop designed for creators and professionals alike. It features a 14.4″ touchscreen and a cutting-edge design that is over

5G Innovations

GPU-Accelerated 5G in Japan

NTT DOCOMO, a global telecommunications giant, is set to break new ground in the industry as it prepares to launch a GPU-accelerated 5G network in Japan. This innovative approach will

AI Ethics

AI Journalism: Balancing Integrity and Innovation

An op-ed, produced using Microsoft’s Bing Chat AI software, recently appeared in the St. Louis Post-Dispatch, discussing the potential concerns surrounding the employment of artificial intelligence (AI) in journalism. These

Savings Extravaganza

Big Deal Days Extravaganza

The highly awaited Big Deal Days event for October 2023 is nearly here, scheduled for the 10th and 11th. Similar to the previous year, this autumn sale has already created

Cisco Splunk Deal

Cisco Splunk Deal Sparks Tech Acquisition Frenzy

Cisco’s recent massive purchase of Splunk, an AI-powered cybersecurity firm, for $28 billion signals a potential boost in tech deals after a year of subdued mergers and acquisitions in the

Iran Drone Expansion

Iran’s Jet-Propelled Drone Reshapes Power Balance

Iran has recently unveiled a jet-propelled variant of its Shahed series drone, marking a significant advancement in the nation’s drone technology. The new drone is poised to reshape the regional

Solar Geoengineering

Did the Overshoot Commission Shoot Down Geoengineering?

The Overshoot Commission has recently released a comprehensive report that discusses the controversial topic of Solar Geoengineering, also known as Solar Radiation Modification (SRM). The Commission’s primary objective is to

Remote Learning

Revolutionizing Remote Learning for Success

School districts are preparing to reveal a substantial technological upgrade designed to significantly improve remote learning experiences for both educators and students amid the ongoing pandemic. This major investment, which

Revolutionary SABERS Transforming

SABERS Batteries Transforming Industries

Scientists John Connell and Yi Lin from NASA’s Solid-state Architecture Batteries for Enhanced Rechargeability and Safety (SABERS) project are working on experimental solid-state battery packs that could dramatically change the

Build a Website

How Much Does It Cost to Build a Website?

Are you wondering how much it costs to build a website? The approximated cost is based on several factors, including which add-ons and platforms you choose. For example, a self-hosted

Battery Investments

Battery Startups Attract Billion-Dollar Investments

In recent times, battery startups have experienced a significant boost in investments, with three businesses obtaining over $1 billion in funding within the last month. French company Verkor amassed $2.1