Develop J2EE applications based on JBoss

xiaoxiao2021-03-06  41

(Author: Luo Shifei) JBoss, J2EE application server as its EJB container trend of superior performance, technology, J2EE application development and deployment convenience to win the trust of many J2EE developers. Among them, several features such as JMX architecture, hot deployment (Hot deploy), rapid development EJB applications are like other commercial servers. Although it does not have a major defect, after all, it is the development mode of Open Source, there is very little document, so it is very good to master, and the development of JBoss-based applications is still not from the heart.

This article combines its own development experience, gives some relevant precautions and rules for J2EE under JBoss 3.2.1. Among them, readers must know JBoss 3.2.1 as a transition product of JBoss (compared to JBoss 3.0.x, JBoss 4.x), natural things and JBoss 3.0.x, JBoss 4.x have a big difference. However, in general, the content introduced herein is generally suitable for JBoss versions.

After downloading JBoss 3.2.1, you can run JBoss without a space-free directory path, so it is convenient, provided that the target machine has installed Java 2 Standard Edition. Start our journey after everything is ready.

(Suppose JBoss 3.2.1 is installed in: c: /jboss-3.2.1_tomcat-4.1.24, this use default configuration)

First, the settings for related configuration files

In order to develop J2EE applications, the operation database has an essential content; the detailed level of adjustment log output is the key to debug J2EE applications; EJB applications are the core of J2EE applications. Wait, these contents are what we need to know.

(1) Configuration of data source:

In JBoss 3.2.1, the steps to configure the data source are simple, JBoss 3.2.1 itself has a configuration instance of the mainstream database, under the directory: c: /jboss-3.2.1_tomcat-4.1.24/docs/examples/jca . Specific use of that profile depends on the database of the target user. If you are SQL Server 2000, you need to use the MSSQL-DS.XML file (support local transaction) or MSSQL-XA-DS.XML file (support global transaction); if it is an Oracle 9i database, you need to use Oracle-DS.xml files. Or Oracle-Xa-DS.xml file. and many more. Here is as an example of SQL Server 2000.

First copy the MSSQL-DS.XML file to the directory: c: /jboss-3.2.1_tomcat-4.1.24/Server/default/deploy under. Then open the file and make the following modification:

VSSDB

JDBC: Microsoft: SQLServer: //125.16.45.158: 1433; DatabaseName = DDD

com.microsoft.jdbc.sqlserver.sqlserverdriver

SA

SA 50

200

If the target J2EE application requires only local transactions, the above process has completed the DataSource configuration, and this configuration will be used for JDBC and EJB through JNDI. If you want to implement the EJB to use DataSource, you also need to modify the directory: c: /jboss-3.2.1_tomcat-4.1.24/server/default/conf under the StandardJbossCMP-JDBC.XML file. such as,

Java: / vssdb1

ms sqlserver2000

True

false

false

300

True

false

. . . . . . . .

The VSSDB in Java: / VSSDB is the data source configured by MSSQL-DS.XML; and "java: /" prefix indicates that the namespace is only visible to JBoss itself, that is, running outside JBoss. The application is not available to use the data source defined here, this is desirable to pay attention.

Second, the MS SQLServer 2000 in MS SQLServer2000 can be found elsewhere in the file. (If you are other databases, the situation is similar, I hope the reader think!)

(2) The output detail of the log of the log:

Since JBoss 3.2.1 developed LOG4J management its log information (strictly speaking, it expands log4j), understanding the log4j mechanism, helps understand the way JBoss 3.2.1 management logs.

JBoss 3.2.1 uses a JMX architecture, and with the .xml file type as a configuration file, you can find the log4j.xml file in the directory: c: /jboss-3.2.1_tomcat-4.1.24/server/default/conf . For example, one of the configuration examples are as follows:

For example, in order to adjust the level of the JBoss 3.2.1 console log output (adjusted to debug level), we need to modify value = "info", change INFO to Debug.

If the target reader is developing Entity Beans, you can adjust the Standardjboss.xml file located in the same directory with the log4j.xml file (this file is mainly to provide EJB-related debugging, running, tuning, deployment parameters). If the target reader Entity Beans uses to Standard Cmp 2.x EntityBean, the value of the attribute is changed to TRUE.

Standard Cmp 2.x EntityBean

False

Entity-Rmi-Invoker

false

. . . . . . . . .

After completing the above two steps, the reader can see the JDBC call details emitted by Entity Beans when debuging Entity Beans.

(3) The configuration of the TOMCAT container related parameters:

If the target reader uses JBoss 3.2.1 integrated version of Tomcat 4.1.24, you can be located in the directory by adjustment: c: /jboss-3.2.1_tomcat-4.1.24/server/default/deploy/jbossweb-tomcat.sar Web.xml and directory: c: /jboss-3.2.1_tomcat-4.1.24/server/default/deploy/jbossweb-tomcat.xml files under the JBoss-Service.xml files under the target reader to achieve specific needs.

For example, if you want to change the HTTP service port to 80, you can modify the jboss-service.xml file; if you want to process more file types to process the target J2EE application, you can modify the web.xml file.

(4) The placement of the relevant class library:

If your application involves a third-party library, such as JDBC Driver, you can store these JDBC Driver to directory: c: /jboss-3.2.1_tomcat-4.1.24/server/default/lib. Note, not a directory: c: /jboss-3.2.1_tomcat-4.1.24/lib.

If it is related to the target J2EE application, it can be stored in the target .war (or. Pear), or under the XXX.WAR directory. No matter what situation, you need to follow J2EE specifications. Of course, JBoss 3.2.1 has a lot of configuration files, such as mail-service.xml files that provide email services, and so on. Here is just some of the information, if you have related questions, you can try some content introduced in this article to solve your problem. Thank you.

Second, develop EJB applications

If developing EJB applications, it is recommended to use JBoss as a development server because development, debugging, deployment speed is fast. If other commercial servers are used, their compilation is very slow due to different mechanisms.

If you use Entity Beans technology, you need to know how to do this. First, how many operation portions have the data source of your target system, that is, whether the database is presented outside the Entity Beans. If so, you need to adjust the submission policy of the and the policy.

For example, there are 4 cases of the in JBoss 3.2.1: A, B, C, D. Of course, if there is no such thing as an entry Beans to access the database, you use A to be very sensible. If there is, you need to use the method depending on the specific situation. At the same time, the selection of is related to the policy.

It is possible to use Entity Beans or Entity Beans Methods, which will be used to reduce or eliminate the possibility of deadlocks.

Try to operate the N-party data table structure by using the relationship between 1: N, which can improve the efficiency of the EJB Container.

Among them, this paper focuses on the discussion of EJB transaction processing.

In general, J2EE application servers support JDBC transactions, JTA transactions, and container management transactions. At the same time, it is best not to use the above three transaction types in the program, such as nested JDBC transactions in JTA transactions; . For example, the following is given the code example of the rollback JDBC transaction:

Public void processt (string orderers) {

CONTEXT INITCTX = New InitialContext ();

Javax.sql.datasource DS = javax.sql.datasource) Initctx.lookup

("Java: Comp / Env / JDBC / OrderSDB");

Java.sql.Connection conn = ds.getConnection ();

Try {

Conn.setautocommit (false); / / Change the default submission of JDBC transactions

ORDERNO = CREATEORDER (ORDERS);

UpdateOrderstatus (OrderNo, "Orders Created");

Conn.commit (); // Submit a JDBC transaction

} catch (exception e) {

Try {

Conn.rollback (); // rollback SJDBC transaction

Throw new ejbexception ("Transaction rollback:" E.getMessage ());

} catch (sqlexception sqle) {

Throw new ejbexception ("SQL operation error:" Sqle.getMessage ());}

}

}

(Modify Self, Transactions in J2EE (RedBooks). JDBC transactions in a para-paragraph of BEDF)

The following is a JTA transaction code example:

Public void processorder (string orderMessage) {

UserTransaction Transaction = MySessionContext.getusertransAction (); // Get JTA Transactions

Try {

Transaction.begin (); // Start JTA transaction

ORDERNO = Sendorder (ORDERMESSAGE);

UpdateRDERSTATUS (ORDERNO, "ORDER SENT");

Transaction.commit (); // Submit JTA Transactions

} catch (exception e) {

Try {

Transaction.rollback (); // rollback JTA transaction

} catch (systemException se) {

SE.PrintStackTrace ();

}

Throw new ejbexception ("Transaction rollback:" E.getMessage ());

}

}

(Modify Self, Transactions in J2EE (RedBooks). JTA transaction in a BEAN in PDF)

At the same time, if the session bean uses JTA management transactions, you must do not get JTA transactions through JNDI, otherwise the result is unimaginable; but use similar, "MySessionContext.getusertractions) method is obtained.

Finally, the container management transaction (CMT) can be used. In the use of the CMT, if the declaration allows the container to complete the transaction rollback, the target EJB application must throw the system-level exception, otherwise the container does not meet the ACID of the transaction. In the anomalous class of the package javax.ejb, other exceptions are all application-level exceptions in addition to NosuCheXception and EJBEXCeption belongs to the system-level exception.

The above 3 points is worth reading. Some readers may always encounter their own ACID that has already been declared as "required" in using the EJB CMT, but it is not possible to guarantee the ACID of the transaction. Therefore, it is hoped that the reader pays attention to the different ways of transaction use in J2EE applications. For details, you can refer to the transaction section of J2EE Tutorial book.

In addition,

1) In the development of Entity Beans, you can use the CMP to use the BMP. If the BMP is intended, it is better to use Session Bean JDBC, where you can control your transaction through JTA (if the performance problem is not very important).

2) If the transaction (ie, through the declaration), it is possible to improve the development efficiency, making you more focused on the business logic itself.

3) Try to use the techniques adopted by J2EE specifications. If the target system needs to be transplanted, many technologies unique to JBoss are used, and their transplantation is very troublesome.

Third, develop web application

Under normal circumstances, it is best to use some THIRTY-Part software or Framework. For example, struts, log4j, webmethods, etc. Its advantage is obvious.

For now, Struts' application is basically the standard for developing web applications. The upcoming JSF is very good to achieve complement and integration with Struts. At the same time, JSF will also bring revolution to the rapid development of JSP web front-end applications. In addition, Struts also promoted the development of JSF. The process of developing a web application under JBoss 3.2.1 is similar to other application servers. At the same time, JBuilder 8, 9 has supported Struts 1.1. If the reader needs JBoss 3.2.1, combined with the article, EJB, JSP, Servlet, JavaBean-related instances, you can give me some suggestions.

Fourth, summary

Through these two articles, several basic issues related to the development J2EE application have been developed. The problem involved in developing J2EE applications is a system engineering issue, nor a few articles, and several books can answer clearly. Many content need everyone to explore, learn, and progress in practice.

thank you all. Welcome everyone to give me a letter, discuss J2EE related development, application issues.

转载请注明原文地址:https://www.9cbs.com/read-82878.html

New Post(0)