In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Google App Engine brings Java development 2.0 implementation example analysis, I believe that many inexperienced people do not know what to do, so this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
Java development 2. 0? It sounds like a novel concept, but it is also an old-fashioned and long-talked concept. With the rise of cloud computing and the Java support provided by Google App Engine, IBMDW's Andrew Glover will introduce what this Java development 2.0 is all about and how it is reflected in Google App Engine.
The Java world is like a rich ecosystem of developers, transactions, and (most importantly) applications, most of which has matured over the past decade. The global Java community has invested a great deal of money, time, and mental work on the Java platform, and these contributions have created a huge treasure trove of successful open source business tools, frameworks, and solutions.
Various investments in the Java platform have subtly changed the way Java is developed. Two important trends are rapidly changing the characteristics of Java development:
Make full use of open source tools and frameworks to build applications from top to bottom
Lease (or loan) a variety of application infrastructure to manage the software life cycle, including running the application itself
None of the aspects of Java Development 2.0 that I'm referring to are new or revolutionary, but the implementation technology is mature enough to assemble better applications quickly and cheaply, which is unprecedented in the history of Java technology-one of the major business requirements that the world wants to achieve.
This article opens a new series that will discuss Java development 2.0 in depth. You will learn about building and deploying Web applications using Amazon EC2, using Google's App Engine, leveraging CouchDB (a database called Web), and tools and techniques for assembling, testing, and deploying applications in the short term at the lowest cost to date.
First stop: Google App Engine for Java. I'll introduce the platform through the usual "Hello World" approach, and then show how to use Groovy, Java Data Objects (JDO), and Eclipse plug-in for Google App Engine to create an effective Web application. But before we do that, let's take a quick look at the commercial value of Java development 2.0.
Fast speed and low cost
Fast and cheap were rarely associated with Java development before. In fact, they are often reminiscent of less serious software development-development by small businesses with limited resources. However, the truth is that IT is a cost center for many companies, both large and small, which encourages companies to maximize value while reducing IT costs.
This is where Java development 2. 0 comes into play. By using open source tools, frameworks, and even solutions, enterprises can quickly assemble applications because they do not need to write a lot of code themselves. When I first started developing with Java technology more than a decade ago, the tools and frameworks available to developers were very limited. And these limited tools are not free. You must buy an IDE, a database, an object-relational mapping (ORM) framework (worst of all, you may have to buy a driver to communicate with the database) and, of course, the machine on which the application is deployed. What about now? All (and more) of the content I just listed is available for free and of great quality.
In addition, by borrowing infrastructure (such as that provided by Amazon EC2 or Google App Engine), you can deploy your application at a very low cost (you previously needed to purchase the required infrastructure).
Build, buy or borrow: this is a new question
Many enterprises have prepared a hardware inventory for running applications, such as databases, application servers, change management systems, and defect tracking tools. In this day and age, however, this list can be thrown away and replaced by using the same software package as a service running on someone else's infrastructure.
The entire application stack that the team uses to manage the development process can be loaned out-that is, rented at a small cost-so that the company does not have to buy the hardware needed to run the application. For example, instead of buying a machine to run a change management system (such as Subversion or Git, both of which are open source free products), teams can use shared change management services such as GitHub. Companies that rent out GitHub introduce the cost of hardware assets, so they charge a small fee (usually monthly per user) to other organizations that use Git. The principle of renting software as a service from other providers can be applied to defect tracking, test case management, and requirements management (for example, through Hosted JIRA or Pivotal Tracker).
The same principle can be applied to underlying hardware assets (usually customized) running other software platforms. Enterprises can abandon the underlying hardware for specific Web applications and tend to run applications on hardware provided by Amazon, Google, or other competitors in the field. These enterprises provide the ability to rent hardware to varying degrees, which is sufficient to host applications. In addition, these companies can manage scalability, backup, and even security. Think about it: Amazon and Google solved these (and more) problems a long time ago, and now they are better at dealing with and innovating aspects of running software platforms efficiently (it's true, let's face it).
For example, by using Google's App Engine, an IT company can reduce the overall cost of purchasing infrastructure to run the applications it needs. And these applications can be deployed more quickly because a variety of cross-cutting issues related to application deployment and management have been considered and raised (and probably in a perfect way).
Fast and cheap no longer mean poor quality. Instead, Java development 2. 0 is a strategic approach that envisions a reliable process that focuses on quality.
Use Google App Engine to lighten the burden
Google App Engine is a real platform for building and deploying Java (and Python) Web applications on top of Google's expensive infrastructure. There is no license fee (unless, of course, the software library you choose to use on the infrastructure requires a license) and no upfront cost for bandwidth or storage space. The App Engine infrastructure is initially completely free until you reach a usage threshold-500MB storage space, quoting Google, "providing sufficient CPU and bandwidth for about 5 million of page views per month." It can be said that once you reach the point where Google starts charging, your Web application has obviously generated a lot of traffic (and benefits).
It's easy to get App Engine up and running. Google even provides an Eclipse plug-in that can handle almost anything for you. And the plug-in contains the basic components of the "Hello World" servlet application, which can help you get started with the platform. In its most recent developerWorks article ("Google App Engine for Java: part 1: get up and running!" Rick Hightower introduced you to the entire process of deploying a Hello World application, including screenshots. If you haven't read Rick's article, you can follow these steps:
Create a Google App Engine account (free of charge) by clicking the Sign up link under Getting Started in http://code.google.com/appengine/.
Download Google App Engine plug-in for Eclipse from http://code.google.com/appengine/downloads.html and install it.
Create a new project in Eclipse by clicking the New Web Application Project button; in the dialog box that appears, do not check the Use Google Web Toolkit option. Name the project and the corresponding package you are interested in.
Select the project in the hierarchy and click the Deploy App Engine Project button.
Enter the credentials (what you used to create the App Engine account in step 1).
Associate the local project with the application ID generated when the App Engine account was originally created. (you can have up to 10 ID).
Click the Deploy button. You'll see a lot of text flashing through the Eclipse console (the plug-in does a lot of work in the background, including the classes needed to enhance the excellent data storage services that take advantage of Google). When the screen is stable (and everything works fine), you should see a "Deployment completed successfully" message.
Visit the App Engine account page on Google and find the Versions link on the Google dashboard. You will see your deployed version and the corresponding URL. Click URL, and then click the link to the generated servlet, and you'll see monotonous but reassuring plain text of "Hello, world".
Write less code using Groovlets
You have successfully deployed your first Google App Engine application without writing a single line of code. In fact, if you plan to use App Engine, you will always have to write some code-but keep in mind that you can reuse a lot of existing code to get the job done more easily. This reusable code may be some of the services provided by Google (such as its data storage or Google account services) or open source libraries that are ported to the Google infrastructure. Reusing other people's code means you often write less code-and less code means fewer defects.
One of my favorite open source libraries (and platforms) is Groovy, which always generates fewer lines of code to create effective applications. The Groovy team recently released a version of the platform that uses App Engine, allowing you to use Groovlets instead of servlets to create an effective application in a short period of time. Groovlets are simple Groovy scripts that behave like servlets. Now that you've implemented a servlet that outputs "Hello, world", I'll show how easy it is to do the same thing with Groovlet (you'll see how much code Groovy can reduce).
Deploying Groovlet on App Engine using the Eclipse plug-in requires only a few simple steps:
Download the latest template for Groovy from http://groovy.codehaus.org/Download (version 1.6.3 at the time of this writing).
Locate groovy-all-1.6.3.jar and place it in the war/WEB-INF/lib directory of your App Engine project. By the way, you can place any library your application needs in this directory (I'll give you some notes later).
Add the contents in listing 1 (mapping Groovlets to the specified request) to the web.xml file in the war/WEB-INF directory:
Listing 1. Update the web.xml file to support Groovlets
< servlet> < servlet-name>GroovyServlet
< /servlet-name> < servlet-class>Groovy.servlet.GroovyServlet
< /servlet-class> < /servlet> < servlet-mapping> < servlet-name>GroovyServlet
< /servlet-name> < url-pattern>* .groovy
< /url-pattern> < /servlet-mapping>Add the groovy directory to the WEB-INF directory; this is where the Groovlets is saved. In the groovy directory, create a new file called helloworld.groovy. In this new file, type println "Hello, Groovy baby!"
Update the version of the application (assuming 1-1) and redeploy. Find the corresponding URL through the Google dashboard, then open / helloworld.groovy in the browser, and you'll see that Groovy outputs a hip message on Google's infrastructure.
It's very simple, isn't it? You just need to add Groovy JAR, update the web.xml file, create a new groovy directory, write a Groovlet, and deploy it. Have you also noticed how Groovlet works with the default servlet plug-in with only one line of code? Which one do you want to write and maintain: a large class or a small class with the same behavior?
Groovy + Java = quickly build effective applications
Now, I'll show you how to use Groovy and Google's App Engine to quickly create an effective application. I'll use a simple HTTP page, a Groovlet, and a JDO-enhanced Java class to persist the event (in this case, triathlon). I'll keep it simple here, but you'll see that this application can evolve to include other features that you'll implement in future articles in this series (using different infrastructure and technologies, of course).
Fast JDO
Google App Engine provides the ability to persist data using JDO, and JDO is an Java persistence standard. For most Java developers, persisting data often means saving information in a relational database; however, for Google, the underlying storage mechanism is its Big Table, which is not relational. That is, it doesn't matter: the details of how Google persists specific properties have been largely hidden. It's fair to say that you can use normal Java objects (or Groovy objects, for the purposes of this article) to build an application that can store information like any other application. This is the method of Google, and you must use JDO. Hibernate is undoubtedly the most popular ORM framework for Java, but it cannot be used for App Engine.
JDO is very simple. You will create POJO, an old-fashioned plain Java object (which can be associated with other Java objects), which you declare to be persistent through the @ PersistenceCapable annotation at the class level. Specify the properties of the object to be persisted through the @ Persistent annotation. For example, I want to store the triathlon event (for now, I will focus on the event rather than the various results related to triathlon)-that is, the event has a name (the name of the triathlon), possibly a description (the type of triathlon), and a date. So far, my JDO looks like listing 2:
Listing 2. A simple triathlon event JDO
Import java.util.Date;import javax.jdo.annotations.PersistenceCapable;import javax.jdo.annotations.Persistent;import javax.jdo.annotations.IdentityType;@PersistenceCapable (identityType = IdentityType.APPLICATION) public class Triathlon {@ Persistent private Date date; @ Persistent private String name; @ Persistent private String description;}
No matter which underlying mechanism is used (that is, relational or Google's Big Table), data persistence always involves the concept of key: a way to ensure uniqueness of different aspects of data in order to avoid data collapse. For example, for triathlon, its key can be the name of triathlon. If two triathlon have the same name, you can combine the name and date as the key. No matter how you represent keys through Google App Engine and JDO, you must specify a key in the JDO object through the @ PrimaryKey annotation. You can also choose some strategies for how keys are generated-generated by you or Google. I'll use Google to generate and keep it simple: the key of my triathlon object is represented as a normal Java Long object, and I'll let Google determine the actual value by specifying a value policy. Listing 3 adds a primary key:
Listing 3. Add a primary key for triathlon JDO
Import java.util.Date;import javax.jdo.annotations.IdGeneratorStrategy;import javax.jdo.annotations.PersistenceCapable;import javax.jdo.annotations.Persistent;import javax.jdo.annotations.PrimaryKey;import javax.jdo.annotations.IdentityType;import org.apache.commons.lang.builder.EqualsBuilder;import org.apache.commons.lang.builder.HashCodeBuilder;import org.apache.commons.lang.builder.ReflectionToStringBuilder;@PersistenceCapable (identityType = IdentityType.APPLICATION) public class Triathlon {@ PrimaryKey @ Persistent (valueStrategy = IdGeneratorStrategy.IDENTITY) private Long id; @ Persistent private Date date @ Persistent private String name; @ Persistent private String description; public Triathlon (Date date, String name, String description) {super (); this.date = date; this.name = name; this.description = description;} / /. Setters and getters left out public String toString () {return ReflectionToStringBuilder.toString (this);} public int hashCode () {return HashCodeBuilder.reflectionHashCode (this);} public boolean equals (Object obj) {return EqualsBuilder.reflectionEquals (this, obj);}}
As shown in listing 3, my triathlon JDO has a key managed by the Google infrastructure and adds some standard methods (toString, hashCode, and equals) that are of great help for debugging, login, and appropriate functionality. Instead of writing this myself, I used the Apache commons-lang library. I also added a constructor that makes it easier to create fully initialized objects than calling a large number of setter methods.
I deliberately kept JDO simple, but as you can see, it didn't contain much (that is, to keep it simple, I removed all relationships and ignored the getter and setter methods). All you have to do is model the domain and then decorate the model with some annotations, and then Google does the rest.
After the object is defined to be persistent, there is one last step left. To interact with the underlying data store, you need to use PersistenceManager, a standard JDO class that, as its name implies, saves, updates, retrieves, and deletes objects (very similar to Hibernate's Session objects) in an underlying data store. This class is created by a factory (PersistenceManagerFactory), which is very complex; therefore, Google recommends creating a separate object to manage a single instance of the factory (which returns an appropriate PersistenceManager if you need it). Accordingly, I can define a simple stand-alone object to return an instance of PersistenceManager, as shown in listing 4:
Listing 4. Returns a simple stand-alone object for a PersistenceManager instance
Import javax.jdo.JDOHelper;import javax.jdo.PersistenceManager;import javax.jdo.PersistenceManagerFactory;public class PersistenceMgr {private static final PersistenceManagerFactory instance = JDOHelper.getPersistenceManagerFactory ("transactions-optional"); private PersistenceMgr () {} public static PersistenceManager manufacture () {return instance.getPersistenceManager ();}
As you can see, my PersistenceMgr is very simple. The manufacture method returns an instance of PersistenceManager from a single instance of PersistenceManagerFactory. You'll also notice that there is no Google-specific code or any other code that leverages JDO in listing 4-all references point to standard JDO classes and interfaces.
The two newly added Java objects are in the src directory of my project, and I added the commons-lang library to the war/WEB-INF/lib directory.
With a defined simple triathlon JDO POJO and a convenient PersistenceMgr object, I already have a good starting point. All I need is to be able to capture triathlon information.
Capture data through Web interface
Most Web applications follow the same pattern: capture information through HTML forms and then submit them to server-side resources for processing. Of course, many other technologies are mixed in the process, but the pattern remains the same regardless of the underlying technology or infrastructure. The same is true of Google App Engine-I have coded server-side resources to process saved triathlon data. All that's left is to capture the information-the form-and connect the server side to the form. In the words of Model-View-Controller (MVC), I need a controller (usually a servlet); I'll use Groovlet instead because I want to write less code.
My HTML form is very simple: all I need to do is create a HTML page and use some simple Cascading Style Sheets (CSS) code to create the form, as shown in figure 1, which looks closer to Web 2.0 than the HTML page that appeared in 1998:
Figure 1. A simple HTML form
As you can see from figure 1, the form captures a name, description, and date. However, a date is not simple-it is actually three attributes of a date.
Fast Groovlet
Groovlets makes it easy to write controllers: they require less code and automatically provide the required objects. In Groovlet, you implicitly access HTML requests and responses through request and response objects, respectively. In my Groovlet, I can get all the properties of the submitted HTML form through the request.getParameter ("name") call, as shown in listing 5:
Listing 5. Actual operation of Groovlets
Def triname = request.getParameter ("tri_name") def tridesc = request.getParameter ("tri_description") def month = request.getParameter ("tri_month") def day = request.getParameter ("tri_day") def year = request.getParameter ("tri_year")
The JDO I wrote earlier uses a Java Date object; however, in listing 5, I deal with three different properties of Date. So I need a DateFormat object to convert the combination of month, day, and year into a normal Java Date, as shown in listing 6:
Listing 6. Data formatting
Def formatter = new SimpleDateFormat ("MM/dd/yyyy") def tridate = formatter.parse ("${month} / ${day} / ${year}")
Finally, after getting all the parameters from the submitted HTML form, I can use the code in listing 7 to persist them to the Google infrastructure through my JDO and the PersistenceMgr object in listing 4:
Listing 7. Easy persistence with JDO
Def triathlon = new Triathlon (tridate, triname, tridesc) def mgr = PersistenceMgr.manufacture () try {mgr.makePersistent (triathlon)} finally {mgr.close ()}
It's that simple! Of course, as more pages are added to my simple application (such as capturing the results of a particular triathlon), I may need to forward or redirect to another form, which will capture additional information, much like a wizard. In any case, with a few short code snippets, I quickly assembled a simple Web application that persisted data to the Google infrastructure through JDO (using normal Java coding) and a Groovlet (using Groovy coding, of course). Deploying the application is as simple as specifying a version in the appengine-web.xml file and clicking the Deploy button.
However, the one-form Web application used to capture triathlon events is not trying to achieve all the functionality, so I'm just deploying the application to an irregular, ubiquitous environment. I don't need to trigger a Web container or even specify where to deploy the application. (is it on California, my hard drive or the moon? The beauty is that it doesn't matter-Google handles the problem. Note that it is to solve all problems. In addition, it is certain that Google already knows how to scale globally so that users in India will have the same experience when viewing applications as users in Argentina.
To sum up, you really need to keep something in mind. Google's infrastructure supports Java technology, but it doesn't mean everything; if you recall the advent of J2ME many years ago, the limitations of App Engine may be similar in nature. That is, not all core Java libraries and related open source libraries are supported. As mentioned earlier, Hibernate is not supported (mainly because you cannot have a relational database when using App Engine). I also encountered some challenges when using some open source libraries with built-in base64 coding (Google requires you to use its URL Fetch service). App Engine is a platform-you have to develop in its direction, which, for now, is an one-way process.
Google App Engine is a platform for the future-suppose you try it out in its sandboxie. Note that I only introduced some of the features in sandboxie; App Engine has a lot of features. If you want more flexibility (that is, you want to have a relational database and you must use Hibernate), but you also want to borrow someone else's extensible infrastructure, you can use an alternative solution. Amazon's EC2 is actually a virtual server on a hybrid infrastructure that is used on demand.
After reading the above, have you mastered the method of sample analysis of Java development 2.0 implementation brought by Google App Engine? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.