Java Development-JBoss-based J2EE applications

Source: Internet
Author: User
Java Development-JBoss-based J2EE application-general Linux technology-Linux programming and kernel information. For details, refer to the following section. JBoss, as a J2EE application server, has won the trust of many J2EE developers with its excellent EJB container performance, technical fluency, and convenient development and deployment of J2EE applications. Compared with other commercial servers, the features such as installation-free, JMX-based architecture, Hot Deploy, and quick development of EJB applications are somewhat disappointing. Although it has no major defects, it is a development mode of Open Source with few documents. Therefore, it is still a little effort to master and master JBoss-based applications.


Based on your development experience, this article provides some precautions and rules for developing J2EE under JBoss 3.2.1. Among them, the reader must know that JBoss 3.2.1 is a transitional product of JBoss (with JBoss 3.0.x and JBoss 4. x). Naturally, there are some things similar to JBoss 3.0.x and JBoss 4. x is quite different. However, the content described in this article is generally applicable to different JBoss versions.


After downloading JBoss 3.2.1, decompress it to a directory path without spaces to run JBoss. Therefore, it is very convenient, provided that the target machine has installed Java 2 Standard Edition. When everything is ready, start our journey.


(Assuming JBoss 3.2.1 is installed in: C: \ jboss-3.2.1_tomcat-4.1.24, this uses the default configuration)


?? 1. Configuration File Settings

To develop J2EE applications, database operations are essential. Adjusting the log output details becomes the key to debugging J2EE applications. The optimization process of EJB applications is the core of J2EE applications. And so on. We need to know the content.


?? (1) Data Source Configuration:


In JBoss 3.2.1, the steps to configure the data source is very simple, JBoss 3.2.1 itself with the mainstream Database Configuration instance, in the directory: C: \ jboss-3.2.1_tomcat-4.1.24 \ docs \ examples \ jca. The specific configuration file used depends on the target user's database. For SQL Server 2000, you need to use a mssql-ds.xml file (supporting local transactions) or a mssql-xa-ds.xml file (supporting global transactions); for an Oracle 9i Database, you need to use a oracle-ds.xml file or a oracle-xa-ds.xml file. And so on. SQL Server 2000 is used as an example.


First copy the mssql-ds.xml file to the directory: C: \ jboss-3.2.1_tomcat-4.1.24 \ server \ default \ deploy. Open the file and make the following changes:


?? <Datasources>

??? <Local-tx-datasource>

??? <Jndi-name> VSSDB </jndi-name>

??? <Connection-url> jdbc: microsoft: sqlserver: // 125.16.45.158: 1433; DatabaseName = DDD </connection-url>

??? <Driver-class> com. microsoft. jdbc. sqlserver. SQLServerDriver </driver-class>

??? <User-name> sa </user-name>

??? <Password> sa </password>

??? <Min-pool-size> 50 </min-pool-size>

??? <Max-pool-size> 200 </max-pool-size>

??? </Local-tx-datasource>

?? </Datasources>


If the target J2EE application only needs local transactions, the configuration of Datasource is completed in the preceding process, and this configuration will be used for JDBC and EJB through JNDI. If you want to implement EJB to use Datasource, you also need to modify the jboss-3.2.1_tomcat-4.1.24 file that is located under the Directory: C: \ standardjbosscmp-jdbc.xml \ server \ default \ conf. For example,


<Jbosscmp-jdbc>


<Defaults>

<Datasource> java:/VSSDB1 </datasource>

<Datasource-mapping> MS SQLSERVER2000 </datasource-mapping>

<Create-table> true </create-table>

??? <Remove-table> false </remove-table>

<Read-only> false </read-only>

<Time-out> 300 </time-out>

<Pk-constraint> true </pk-constraint>

<Fk-constraint> false </fk-constraint>

???........


Among them, the VSSDB in <datasource> java:/VSSDB </datasource> is the data source of the mssql-ds.xml configuration, and the "java:/" prefix indicates that the namespace is only visible to the JBoss itself, that is, applications running outside JBoss cannot use the data sources defined here.


Secondly, the MS SQLSERVER2000 in <datasource-mapping> MS SQLSERVER2000 </datasource-mapping> can be found elsewhere in the file. (For other databases, the situation is similar. I hope readers can think about it !)


?? (2) log output details Configuration:


Since JBoss 3.2.1 is developed using Log4j to manage its log information (strictly speaking, it extends Log4j), understanding the mechanism of Log4j helps to understand how JBoss 3.2.1 manages logs.


JBoss 3.2.1 adopts the JMX architecture and. the xml file type is the configuration file, so you can find the log4j in the directory: C: \ jboss-3.2.1_tomcat-4.1.24 \ server \ default \ conf. xml file. For example, a configuration example is as follows:


?? <Appender name = "CONSOLE" class = "org. apache. log4j. leleappender">

??? <Param name = "Target" value = "System. out"/>

??? <Param name = "Threshold" value = "INFO"/>

??? <Layout class = "org. apache. log4j. PatternLayout">

???? <! -- The default pattern: Date Priority [Category] Message \ n -->

???? <Param name = "ConversionPattern" value = "% d {ABSOLUTE} %-5 p [% c {1}] % m % n"/>

??? </Layout>

?? </Appender>


For example, to adjust the log output details of the JBoss 3.2.1 Console (adjusted to the DEBUG level), we need to modify value = "INFO" and Change INFO to DEBUG.


If the target reader is developing Entity Beans, you can adjust. standardjboss in the same directory as the xml file. xml file (this file mainly provides modification of EJB-related debugging, running, tuning, and deployment parameters ). If the target reader Entity Beans uses the <container-name> Standard CMP 2.x EntityBean, change the value of the <call-logging> attribute to true.


?? <Container-configuration>

?? <Container-name> Standard CMP 2.x EntityBean </container-name>

?? <Call-logging> false </call-logging>

?? <Invoker-proxy-binding-name> entity-rmi-invoker </invoker-proxy-binding-name>

?? <Sync-on-commit-only> false </sync-on-commit-only>

??.........


After completing the preceding two steps, you can view the JDBC call details sent by Entity Beans on the console when debugging Entity Beans.


?? (3) configuration of Tomcat container parameters:


If the target reader uses the integrated version of JBoss 3.2.1 and Tomcat 4.1.24, you can adjust the web in the directory: C: \ jboss-3.2.1_tomcat-4.1.24 \ server \ default \ deploy \ jbossweb-tomcat.sar. xml and Directory: C: \ jboss-3.2.1_tomcat-4.1.24 \ server \ default \ deploy \ jbossweb-tomcat.sar \ META-INF under the jboss-service.xml file to meet the specific needs of the target reader.


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


?? (4) Placement of related class libraries:


If your application involves third-party class libraries such as JDBC Driver, you can store these JDBC drivers in the directory C: \ jboss-3.2.1_tomcat-4.1.24 \ server \ default \ lib. Note that it is not under the 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. ear), or in the WEB-INFO \ lib directory of xxx. war. In either case, the J2EE specifications must be followed.


Of course, there are a lot of configuration files for JBoss 3.2.1, such as mail-service.xml files that provide mail services, and so on. Here, we will only provide you with some information. If you have any questions, you can try to solve your problems through the content described in this article. Thank you.


?? 2. Develop an EJB application


If you develop an EJB application, we recommend that you use JBoss as the development server because it is fast in development, debugging, and deployment. If other commercial servers are used, the Compilation speed is slow due to different implementation mechanisms.


If Entity Beans technology is used, you need to know the following points. First, how many operation portals are available for the data source of your target system, that is, whether there are methods beyond Entity Beans to operate the database. If yes, you need to adjust the <commit-option> submission policy and <locking-policy> policy of the corresponding <container-name>.


For example, JBoss 3.2.1 adopts four <commit-option> Methods: A, B, C, and D. Of course, if you have no access to the database except Entity Beans, it is rational to use. If yes, you must use the <commit-option> method based on the actual situation. In addition, the selection of the <commit-option> method is related to the <locking-policy> policy.


If you can use <read-only> Entity Beans or Entity Beans Methods, try to use them. This will reduce or eliminate the possibility of deadlock.


Try to use the 1: n relationship to operate the n-party data table structure, which can improve the efficiency of EJB Container.


This article focuses on EJB transaction processing.


Generally, the J2EE Application Server supports 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 at the same time, such as nesting JDBC transactions in JTA transactions; second, the transaction should be completed in the shortest time, do not use transactions in different methods. For example, the following is a sample code for rolling back a JDBC transaction:


?? Public void processT (String orders ){

??? 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 commit method of JDBC transactions

???? OrderNo = createOrder (orders );

???? UpdateOrderStatus (orderNo, "orders created ");

???? Conn. commit (); // submit a JDBC transaction

???} Catch (Exception e ){

???? Try {

????? Conn. rollback (); // roll back sJDBC transactions

????? Throw new EJBException ("transaction rollback:" + e. getMessage ());

????} Catch (SQLException sqle ){

????? Throw new EJBException ("SQL operation error:" + sqle. getMessage ());

????}

???}

??}


?? (Modified from a JDBC transaction managed by a Bean in Transactions in J2EE(RedBooks).pdf)


?? The following is an example of JTA transaction code:


?? Public void processOrder (String orderMessage ){

??? UserTransaction transaction = mySessionContext. getUserTransaction (); // obtain the JTA transaction

??? Try {

???? Transaction. begin (); // start the JTA transaction

???? OrderNo = sendOrder (orderMessage );

???? UpdateOrderStatus (orderNo, "order sent ");

???? Transaction. commit (); // submit a JTA transaction

????} Catch (Exception e ){

????? Try {

?????? Transaction. rollback (); // roll back a JTA transaction

?????} Catch (SystemException se ){

?????? Se. printStackTrace ();

?????}

???? Throw new EJBException ("transaction rollback:" + e. getMessage ());

???}

??}


?? (Modified from a JTA transaction managed by a Bean in Transactions in j2ee(redbooks)


At the same time, if Session Bean uses JTA to manage transactions, do not obtain JTA transactions through JNDI; otherwise, the results will be unimaginable; instead, it will be obtained using a method similar to "mySessionContext. getUserTransaction.


Finally, you can use the container to manage transactions (CMT ). When using CMT, if the container is declared to complete transaction rollback, the target EJB application must throw a system-level exception. Otherwise, the container will not meet the ACID of the transaction. In the exception class of the javax. ejb package, except NoSuchEntityException and EJBException are system-level exceptions, all other exceptions are application-level exceptions.


The above three points are worth your attention. Some readers may encounter ACID that they have declared the transaction as "Required" when using the ejb cmt, but cannot guarantee the transaction. Therefore, you need to pay attention to the different transaction methods used in J2EE applications. For details, refer to the transaction processing chapter in J2EE Tutorial.


?? In addition,


?? 1) when developing Entity Beans, try not to use BMP when using CMP. If the BMP method is to be used, it is better to use Session Bean + JDBC, where the transaction can be controlled through JTA (if the performance problem is not very important ).

?? 2) use the transactions (namely, declarations) implemented by containers as much as possible to improve development efficiency, so that you can focus more on the business logic itself.

?? 3) try to use the technologies used in J2EE specifications. If the target system needs to be transplanted, but many JBoss-specific technologies are used, the porting is very troublesome.


?? 3. Develop Web Applications


Generally, it is best to use Thirty-Part software or Framework to develop Web applications. For example, Struts, Log4j, and webMethods. Its advantages are obvious.


Currently, Struts applications are basically the standard for developing Web applications. The coming JSF will provide a good way to complement and integrate it with Struts. At the same time, JSF will bring a revolution to the rapid development of JSP Web front-end applications. In addition, Struts also promotes the development of JSF.


The process of developing Web applications under JBoss 3.2.1 is similar to that on other application servers. JBuilder 8 and 9 support Struts 1.1. If you need articles on Struts, EJB, JSP, Servlet, and JavaBean related instances in JBoss 3.2.1, you can give me some suggestions.


?? Iv. Summary


Through these two articles, several basic issues related to J2EE application development are preliminarily discussed. The question involved in the development of J2EE applications is a system engineering question. It is not a few articles or several books that can answer clearly. A lot of content needs to be explored, learned, and improved in practice.
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.