Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is it like to accelerate Google App Engine development based on Groovy

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail what it is like to accelerate Google App Engine development based on Groovy. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

Lightweight is a whole new trend.

While most of Google's infrastructure is free (charged when storage space and bandwidth reaches 500MB and about 5 million page views per month), it sacrifices some flexibility. Google's infrastructure supports Java technology, but this does not include all the core Java libraries and related open source libraries. App Engine is a platform-you need to develop on it. It is not surprising, however, that open source innovation will help overcome obstacles to the adoption of Google App Engine.

The Gaelyk framework is a typical example of such a project, which is designed to accelerate the development of lightweight applications, that is, applications developed using Groovy that make proper use of the Model-View-Controller (MVC) pattern. Under the magic of Groovy, Gaelyk will inject some ease of use factors into App Engine's API. In addition, you can use Gaelyk in conjunction with the Google App Engine plug-in for Eclipse. Rapid development and deployment of Google App Engine applications will be very simple.

"REST through CouchDB and Groovy's RESTClient" demonstrates the features of a document-oriented database using a parking ticket system. In this article, I will continue to create a Web application that supports the creation, update, and deletion of parking tickets. The Google persistence architecture is not document-oriented, but its schemaless nature implements a more flexible model. Therefore, Web will build a parking ticket model as accurately as possible, which requires obtaining:

Name of police officer

Date

Position

Violation situation

Any relevant comments

I keep the location as a normal text box because some people can use a variety of ways to indicate where the violation occurred-such as in the parking area of Best Buy or at the corner of 18th Street and D Street. In fact, I won't try to describe a particular format because it doesn't necessarily fit this domain.

First, you need to install the Google App Engine plug-in for Eclipse. You also need to download the Gaelyk JAR file from the project's website. Remember the location of the download, because you then need to move it to a specific directory.

The Gaelyk framework depends on Groovy, so you also need a Groovy distribution of * *: at the time of this writing, it is a simple JAR file, groovy-all-1.6.5.jar. *, you need to create an application ID through the Google App Engine management panel. If you prefer, you can reuse the application ID created in "using Google App Engine".

Next, create a new Google Web Application Project in Eclipse, click the Next button and fill in the appropriate information. Make sure the Use Google Web Toolkit option is unchecked, as shown in figure 1, because you don't need it:

Figure 1. Create a Google Apps Project in Eclipse

Click the Finish button and you will build the code base.

Now, copy Groovy and Gaelyk JAR to the war/WEB-INF/lib directory of the newly created project, as shown in figure 2:

Figure 2. Required libraries for Gaelyk

To configure Gaelyk, you need to provide some additional information for Google App Engine by editing the WEB-INF/appengine-web.xml file. Add your application ID to the application section at the top of this file, and add some XML, as shown in listing 1:

Listing 1. Necessary updates to the App Engine configuration

Adding this content will prevent Google App Engine from statically providing the various files that will eventually be created when using Gaelyk. As you can see, Gaelyk will leverage a template model. Therefore, files with the .gtpl extension will be similar to JavaServer Pages (JSP) and will be processed through the framework rather than App Engine.

Next, open the web.xml file. This file can also be found in the WEB-INF directory. It is a standard Web application configuration file. (you will manipulate this file the first time you access App Engine and EC2. This file needs to map various schemas to a specific servlet, so adjust your file according to listing 2:

Listing 2. Updated web.xml file

GroovletServlet groovyx.gaelyk.GaelykServlet TemplateServlet groovyx.gaelyk.GaelykTemplateServlet GroovletServlet * .groovy TemplateServlet * .gtpl index.gtpl

Note that the web.xml file specifies that the welcome file is index.gtpl;, so rename the index.html file that the Eclipse plug-in generated for you to index.gtpl. (if you are using the Windows ®operating system, simply select the file and press F2. )

With the appropriate libraries in place and the two XML files configured correctly, you can verify that everything is working properly by editing the index.gtpl file to match the contents of listing 3:

Listing 3. A simple GTPL file

A Simple GTPL

% >

Comparison and Analysis of Groovy and Java

Google App Engine performance optimization strategy: pageness

Java Development 2.0 implementation brought by Google App Engine

Java persistence and data Storage of Google App Engine

JVM popular dynamic language Groovy 1.7 released

As you can see, the GTPL file (or Gaelyk/Groovy template) in Gaelyk is similar to JSP: you can add a behavior in scriptlet (in this case, the added behavior is Groovy). Note that you can also use closures and reference variables later.

Save your index.gtpl file select the base directory of the project in Eclipse, right-click, select Run As, and select the Web Application option that contains the blue G logo, as shown in figure 3:

Figure 3. Run as a Google Web application

By default, this launcher starts a local instance of Jetty on port 8080. If you want to change the port, select the Run Configurations option and configure the port through the options panel provided by the plug-in.

After running the local instance of the Gaelyk Web application, open a Web browser and access http://localhost:8080. The output of the index.gtpl should look like figure 4:

Figure 4. Hello world!

It's very simple, isn't it?

Simple persistence

The ticket system is very simple. It provides an Web form that can be used to create tickets and to view, delete, and edit tickets in the list. I'll first create a simple HTML form from the Gaelyk template and name it createticket.gtpl. As shown in figure 5, this form attempts to capture data related to each parking ticket:

Figure 5. A simple ticket form

The form will be submitted to a groovlet;, creating a groovy folder in the project's WEB-INF directory accordingly. Your groovlet will be stored here. (you also did this in using Google App Engine. The create ticket (create-ticket) form will be submitted to an createticket.groovy file. Create this file in the newly created groovy directory.

There is no doubt that you can use JDO and Java Persistence API (JPA) code in Gaelyk, but there is another way to interact with the underlying data store: using Google's Entity object. The Gaelyk team has enhanced Entity objects by simplifying the manipulation of persistent objects with some Groovy magic.

In this case, I want to get the form elements submitted through the createticket.gtpl page and create a new ticket in the system. By using the Entity class, I don't need to define an object similar to POJO to represent the ticket (as I did when I created the Triathlon JDO object in "using Google App Engine"). I can use Groovy to build a ticket model and save it effortlessly.

Therefore, I can get the parameters submitted by the form through Gaelyk's params object (which Grails also provides in some form) and create an instance of Entity, as shown in listing 4:

Listing 4. Create an Entity

Def formatter = new SimpleDateFormat ("MM/dd/yyyy") def offensedate = formatter.parse ("${params.of_month} / ${params.of_day} / ${params.of_year}") def ticket = new Entity ("ticket") ticket.officer = params.officerticket.license = params.plateticket.issuseDate = offensedateticket.location = params.locationticket.notes = params.notesticket.offense = params.offense

Note that the ticket variable is an instance of Entity. The "ticket" String represents such an entity. It is very convenient to search for tickets. Next, I will automatically assign attribute values to the Entity instance associated with the ticket. Ticket.officer now represents the value of the officer parameter submitted through the Web page form. Because the form contains three date fields, I also use SimpleDateFormat to create a date instance and set the value to issueDate.

At this point, I have created an object to represent the ticket. Now, all I have to do is save it with the following code:

Ticket.save ()

Now that I have persisted a ticket, I will direct the user to a page where the ticket can be viewed. It's also very simple. I just need to direct to view-ticket Groovlet (for processing):

Redirect "viewticket.groovy?id=$ {ticket.key.id}"

As you can see, I have created a parameter named id and set it to the key of the saved ticket (generated by Google App Engine) instance. As you can see, create-ticket Groovlet is very concise and powerful-thanks to Gaelyk.

Simple view

In the previous example, after I created the ticket instance, I continued to redirect the request to another Groovlet-which simplifies the process of viewing the ticket. In this Groovlet, I wrote a Google App Engine "read". The passed id will be used to find the newly created instance. In this case, I will use Google's KeyFactory, which is used to create an instance of the Key object of Google. Key will then be used to find the appropriate ticket instances through datastoreService, and Gaelyk has added them to any Groovlet instance in the framework, as shown in listing 5:

Listing 5. View Entity

Import com.google.appengine.api.datastore.KeyFactoryif (params ["id"]) {def id = Long.parseLong (params ["id"]) try {def key = KeyFactory.createKey ("ticket", id) def ticket = datastoreService.get (key) request.setAttribute "ticket", ticket forward "viewticket.gtpl"} catch (Throwable t) {/ / forward to some error page... }} else {forward "index.gtpl"}

Once the appropriate ticket is found, the ticket is added to the HTTP request object (which already appears in the Groovlet), and the processing is transferred to the viewticket.gtpl page. Like any other JSP in the Web application, this Web page will display the corresponding properties related to the incoming ticket.

As you can see from listing 6, Gaelyk supports includes. That is, in your .gtpl file, you can include other files, just like normal JSP files. Similarly, all .gtpl files have an instance of the HTTP Request object available (through the request variable).

Listing 6. View Entity GTPL

Parking Ticket Issuing OfficerVehicle PlateDateOffenseLocationNotes ${ticket.officer} ${ticket.license} ${ticket.issuseDate} ${ticket.offense} ${ticket.location} ${ticket.notes}

At this point, you may find that Gaelyk makes it easy to create lightweight Web applications on Google App Engine. Also, it's easy to manipulate App Engine's persistence library. The low-level API you use when working with Entity does take some time to get used to. Queries require some thinking (similar in some ways to executing queries using CouchDB). For example, viewing a list of created tickets requires some code like the one shown in listing 7:

Listing 7. View a set of Entity

Import com.google.appengine.api.datastore.Queryimport static com.google.appengine.api.datastore.FetchOptions.Builder.withLimittry {def query = new Query ("ticket") query.addSort ("issuseDate", Query.SortDirection.DESCENDING) def preparedQuery = datastoreService.prepare (query) def tickets = preparedQuery.asList (withLimit (10) request.setAttribute "tickets", ticketsforward "index.gtpl"} catch (Throwable t) {forward "index.gtpl"}

Listing 7 uses the Query object of App Engine. As you can see, you can add features similar to sorting to the query, and even limit the way the results are returned. You don't need to use SQL, but you need to make sure that the data is stored and retrievable, with only a few differences.

Comparison and Analysis of Groovy and Java

Google App Engine performance optimization strategy: pageness

Java Development 2.0 implementation brought by Google App Engine

Java persistence and data Storage of Google App Engine

JVM popular dynamic language Groovy 1.7 released

As described in "using Google App Engine", the process of deploying to the cloud is also very simple. With the plug-in, just click Deploy App Engine Project and the rest is done by Google. In fact, you can download the code for this article to do this. The code will fill in some gaps, so I can't list all the code in one article. For example, I've implemented the ability to delete tickets, and the interaction between users and tickets has been slightly enhanced, so you can feel the effect of Gaelyk more or less.

Rapid development is easier

Cloud and modeless data storage supported by open source technologies are undoubtedly part of future Java development. The barriers to adoption for both are low; in the examples in this article, both hardware and software are free. And once Google starts charging, it must be self-reliant-5 million hits per month is a huge amount of traffic. The Gaelyk framework accelerates the pace of Web development. Java development is always improving, isn't it?

The above is how it is to accelerate Google App Engine development based on Groovy. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report