Calling ejbs in ASP. NET Applications

Source: Internet
Author: User
Tags jboss application server

  SummaryThis article describes how to implement interaction between your ASP. NET application and J2EE application server, and how to make it as easy to call EJB as to call XML Web Services.

  Introduction

Today, many large organizations have discovered that they are owning and using a development team and release server composed of a mix of. NET and J2EE technologies. To balance the quality of enterprises sent through J2EE application servers, the company's business logic is often published on J2EE application servers in the form of Enterprise JavaBean (EJB. On the other hand, to meet the changing business development requirements, most developers prefer to implement the description logic in A Visual Studio. NET development environment with high production performance. When you need to connect the. Net Description layer to the J2EE business logic layer, the challenge arises.

Let's take a look at how visual mainwin for J2EE helps you face and overcome it-from Visual Studio.. net, use C # Or Visual Basic. net to implement ASP. net Description layer and then calls the commercial logic layer implemented in the form of EJB-this challenging development. You can do this without having to get stuck with the ejb api encoding. With the help of visual mainwin for J2EE, two layers-Asp. net front-end and EJB backend-both of which can be used as a pure J2EE application to recite the failure on the 2ee application server, and performs performance optimization and achieves the same J2EE-based security.

To call ejbs from Visual Studio. NET, you must install the Enterprise Edition of visual mainwin for J2EE. Of course, you can download its evaluation version from the mainsoft.com website for experimental analysis.

  Sample Analysis

The stocksportfolio example in this article-it shows you how to use an ASP.. NET web layer and a J2EE business layer to create applications-installed and documented in visual mainwin for J2EE. In this example, a simple ASP. NET web page is used by users to manage their stock investment and an ASP. NET web service is used to provide fictitious stock quotations. In this example, a session EJB is used to implement the logic of stock trading for your J2EE application server.


Figure 1. stocksportfolio application running on the JBoss Application Server

  Add EJB to your. Net Environment

Calling an EJB from Visual Studio. NET is as simple as calling a web service. In your Solution Explorer, right-click "references" and select "add EJB reference". A new reference type that is only available for the visual mainwin for J2EE project appears, it is similar to the Standard Visual Studio. web reference in netide (see figure 2 ).


Figure 2. Add an EJB reference

To add an EJB reference to your visual mainwin for J2EE project, you only need a Java file (jar) that implements the EJB or contains its local and remote interfaces. Visual mainwin can query information about all ejbs published on the application server and display the ejbs corresponding to your jar definition in a dialog box. You only need to select the specific EJB you want to use (maybe multiple) (see figure 3 ).


Figure 3. Add an EJB reference dialog box

You can also consume the EJB on a remote application server, as long as it is of the same type as the local application server related to your project. This may be a windows, Linux, UNIX, main framework, or any other server that supports J2EE. To consume an EJB published on a remote server, click "advanced" to expand the dialog box (see figure 4 ).


Figure 4. Add EJB reference dialog box in Advanced Mode

Enter the jndi url to the remote J2EE application server, and then click "fetch from server ". Visual mainwin will list all ejbs published on the remote server and ejbs corresponding to your jar files. This operation is consistent with that of the local EJB.

Select the EJB you want to consume (maybe multiple) and click OK. A new EJB reference folder is generated in your Solution Explorer, as shown in Figure 5. This folder contains the server-based reference for each newly added EJB reference, similar to the Web reference node. In addition, a packaging class is generated to simplify the EJB call encoding. This packaging class will be discussed later.


Figure 5. Solution Explorer folder with EJB reference

  Call the EJB method from. net

When you add an EJB reference to your project, the system will automatically generate one. net (C # Or VB.. Net) class, which describes a simple interface to the EJB. This class includes the J2EE code that requires the creation of the EJB and the method to call it. This. Net class exposes the method of this EJB remote interface through its own public method. To call your EJB business method, you only need to create an instance of the packaging class and call the appropriate packaging class method.

The following is a sample code for calling an EJB method from your. Net project:

// Create an instance of stocktrader EJB.
Localhost. stocktraderejb trader = new localhost. stocktraderejb ();
// The purchased stock defined by the user in the stock name text box,
// The number of bought shares corresponds to the number in the number text box
Trader. Buy (tbstockname. Text, int32.parse (tbnumofshares. Text ));

  In-depth analysis

Execute the requested J2EE call in the static constructor of the preceding packaging class to create the home object of this EJB. Then, in a default constructor, it uses the home object to create the EJB object. This EJB object is stored as a packaging class member and is used to call commercial EJB methods.

The following code creates the packaging class of the stocktrader EJB:

Private Static trading. stocktraderhome;
Private trading. stocktraderejbobj;
Static stocktraderejb (){
// Create a Java Naming (JNDI) Context
Context context;
Context = vmw. J2EE. j2eeutils. createcontext (null, null );
Object homeobj;
// Obtain the home object from the JNDI Server
Homeobj = context. Lookup ("EJB/stocktrader ");
Home = (trading. stocktraderhome) (homeobj ));
}
// Default constructor: Creates a new EJB instance.
Public stocktraderejb (){
This. ejbobj = home. Create ();
}

This packaging class exposes the method of this EJB remote interface through its public method. Then, each of these methods calls the corresponding commercial method of your EJB through the EJB object. The following code shows you the method in the EJB wrapper of the stock DEALER:

Public Virtual void buy (string arg_0, int arg_1 ){
This. ejbobj. Buy (arg_0, arg_1 );
}
Public Virtual void Merge (string arg_0, int arg_1 ){
This. ejbobj. Values (arg_0, arg_1 );
}

Visual mainwin is also responsible for ing Data Types Between Java and. net. For example, if one of your EJB Methods receives a java. lang. calendar Object, then you will use. net System. datetime object parameters to call this method and map it to a java. lang. calendar Object. Then, if your EJB method returns a java. Lang. class, you will receive a system. Type object instead.

  Debugging problems

Even if visual mainwin simplifies development, you may still need to debug your multi-level, mixed ASP. NET/EJB applications. The visual mainwin debugger allows you to debug your hybrid applications from within Visual Studio. net ide. You can set interruptions in your C # Or VB. NET code through language boundaries, debug EJB Java code in one step, and debug all your applications. Moreover, because debugging needs to occur in any issue, the visual mainwin debugger can be attached to your J2EE application server, regardless of whether it is running on Linux, unix or other frameworks, as long as it can run in debugging mode.


Figure 6. Use the visual mainwin debugger to debug the EJB source code

The application created by visual mainwin for you is a standard J2EE servlet application that can be published and managed through the J2EE Application Server supervisor console, just like any other J2EE servlet application. Therefore, both your asp. Net Description layer and your EJB business logic layer can rely on the same J2EE security infrastructure. Your mixed asp. net/EJB applications can rely on a consistent security model that passes J2EE servlet authentication, your application server users and role definitions can also ensure security by balancing role-based authorization mechanisms.

  Summary

1. This article discusses remote objects and interfaces. It is also possible to consume local objects through visual mainwin. To publish an application that uses a local object, you must create an enterprise file (EAR) that includes both the war file of your application and the JAR file of the local EJB.

2. Although visual mainwin can map most. Net types to Java types, it cannot Map Collection types-because this ing may lead to a performance loss. Therefore, you can choose to process Java Collection types from your. Net code or execute such conversions yourself.

3. Visual mainwin allows you to consume session beans and non-transactional entity beans. The transaction Entity Bean cannot be transparently consumed, so you must manually encode the J2EE transaction call. However, in most cases, transactional entity beans can be accessed through session beans, so you are unlikely to need to do so.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.