Browse DevX
Sign up for e-mail newsletters from DevX


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

Getting your apps ready to interact with a J2EE application server is a daunting task for the average VB.NET or C# developer, but Visual MainWin uses Web services to simplify the process. Find out how and learn to write and consume EJBs using C# and a .NET Server.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

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.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date