Java server-side programming

Source: Internet
Author: User
Tags log4j

To do mobile-side Android program, so use Java to develop. Later saw the Javaweb, think Java is very good, the function is very powerful, can do a lot of things, the most important is the resources are very rich, there are many open-source library framework and so on.

Recently use Java to do a server-side program, so write it down.

is actually a console program, functional and complex, mainly open a socket port, and then a sensor device connected to, and then continue to receive and send data, collect the data to do after the analysis, and then save the data to the database.

Due to the operation of the database, so directly to the web side of the things directly, using spring+hibernate, so that do not have to engage in database links and other things, and Web projects, through Maven to introduce the required jar package, Change the pom.xml used in the Web project to get rid of what you don't need.

It is possible that spring is generally used in Web projects, when doing the console program, packaging out some problems, in the debugging run no problem, but after packaging, using Java-jar way to run the error, mainly because the schema is not found, but fortunately, After a search, the problem was solved quickly.

Use a MAVEN plugin to process the dependent schema file when you package it.

<plugin>    <groupId>Org.apache.maven.plugins</groupId>    <Artifactid>Maven-shade-plugin</Artifactid>    <version>2.1</version>    <executions>        <Execution>            <Phase>Package</Phase>            <Goals>                <goal>Shade</goal>            </Goals>            <Configuration>                <Finalname>----File name-----</Finalname>                <shadedartifactattached>True</shadedartifactattached>                <Shadedclassifiername>Jar-with-dependencies</Shadedclassifiername>                <Transformers>                    <TransformerImplementation= "Org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">                        <MainClass>Com.xxxx.xxxx</MainClass>                    </Transformer>                    <TransformerImplementation= "Org.apache.maven.plugins.shade.resource.AppendingTransformer">                        <Resource>Meta-inf/spring.handlers</Resource>                    </Transformer>                    <TransformerImplementation= "Org.apache.maven.plugins.shade.resource.AppendingTransformer">                        <Resource>Meta-inf/spring.schemas</Resource>                    </Transformer>                    <TransformerImplementation= "Org.apache.maven.plugins.shade.resource.AppendingTransformer">                        <Resource>Meta-inf/spring.tooling</Resource>                    </Transformer>                </Transformers>            </Configuration>        </Execution>    </executions></plugin>
View Code

After joining Spring and Hibernate, the console has a good ability to manipulate the database, which is handy if you use ORM. It is also possible to use the timer function provided by spring, which requires the use of timed tasks.

Server program, the log is to have, use log4j on it, log4j in the general Web projects have, directly take over it can.

After all these things have been configured, the work is actually done in three.

Here again the socket bar, when the customer connected to come, there are several different ways of processing.
1. Put all the client connections into a list, and then open a separate thread, the list of connections to process, such as traversing the list of clients to read the data sent over, there is a disadvantage, that is, when reading the data sometimes if the client network exception thread will block, although you can set the timeout, But reading the line measuring modules in there affects the other client is not very good.
2. When a client is connected, a new thread is opened so that the client does not interfere with each other, but if too many threads are too open, the client will consume more server-side resources.

Because the number of clients is not many, the second method is used.

After the program is written, put to the company's PC on a test run, the next morning to see the night when more than 9 o'clock on the card there is not moving, the first thought is the problem of the socket, changed to put in there to continue to run, the third morning to see or stuck there. And then wrote some test code, found to be the database connection pool problem, due to open a lot of threads, the database connection is not enough to use, so the card is there, changed the C3P0 connection pool settings just fine.

Of course, if the number of concurrent connections is too many for the performance of the database, the CPU frequency will increase. In the current situation, the number of clients is not a problem, if the number of clients is too much, it needs to be handled in other ways, such as 10 or 100 clients to share a thread, the socket and database operations to different threads and so on. Of course, this process will become more complex, need more time to do, if it is to fast delivery, you need to adopt a "simple rough" way.

The general program does not have very high performance requirements, so that to use C or C + + to do development, based on the development efficiency of the use of Java development is very good.

Java server-side programming

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.