Google App Engine 101 v1

Uvod do App Engine... »
Michal Srajer

Google App Engine
101
Michal Šrajer
@srakyi
Google App Engine is a platform for developing and hosting web applications in Google-managed data centers.
Java & Python
PaaS
ale diky JVM i Groovy, JRuby, Scala, Clojure, JavaScript... 
datastore
postaven nad BigTable
frameworks
například Grails, JSF, Spring MVC, Struts, Tapestry, SiteMesh...
APIs
Google Accounts, URL Fetch, Mail, XMPP... 
http://bit.ly/AEtalk-fw
ne IaaS jako EC2
Architecture
+
-
rychlý startup projektu
škálování
cena
snadný vývoj
využití standardních API
postupně roste a zlepšuje se
nutí k vývoji scalable apps
limits & quotas
není k dispozici vše z JRE
whitelist -
read-only filesystem
práce s thready
naked domains
loading requests
http://bit.ly/AEtalk-white
30 sec + 10 MB
dev server
jen opravdu využité zdroje
= start nové instance aplikace (problém s Grails)
The White House's "Open For Questions" application 
accepted 100K questions and 3.6M votes in under 48 hours
registrace
https://appengine.google.com/
vytvoření
app id
stažení
SDK
instalace
pluginů
vytvoření
projektu
deploy
Co to je?
Jak na to?
BigTable is a sparse, distributed, persistent
multidimensional sorted map. 
rychle cteni X pomaly zapis
výběr ze 3 API - JPA, JDO, low-level API

alternativa: Objectify
import com.google.appengine.api.datastore.Key;

import java.util.Date;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

@PersistenceCapable
public class Employee {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;

    @Persistent
    private String firstName;

    @Persistent
    private String lastName;

...}
        PersistenceManager pm = PMF.get().getPersistenceManager();

        Employee e = new Employee("Alfred", "Smith", new Date());

        try {
            pm.makePersistent(e);
        } finally {
            pm.close();
        }

Entity entity = new Entity("myKind");
Email myEmail = new Email("test@email.com");
entity.setProperty("emailProperty", myEmail);
Key key = datastore.put(entity);

Entity storedEntity = datastore.get(key);
Email storedEmail = (Email) storedEntity.getProperty("emailProperty"); 
JCache API
vše v RAM
10x rychlejší než datastore

java.net API
HTTP a HTTPS
GET, PUT, POST, DELETE, HEAD
Google Secure Data Connector
JavaMail API
send (via e-mail admina / usera)
receive (via Servlet)

send, receive, invite...
podobně jako Mail
resize, rotate, crop, flip
composite, convert
enhance, get info

autentizace Google účtem
autorizace admina
background tasks
data = HTTP POST
code = URL
Blob
nově až 2GB
upload na Blobstore -> rewrite

<body>
    <form action="<%= blobstoreService.createUploadUrl("/upload") %>" method="post" enctype="multipart/form-data">
        <input type="file" name="myFile">
        <input type="submit" value="Submit">
    </form>
</body>
Queue queue = QueueFactory.getDefaultQueue();
queue.add(url("/worker").param("key", key))
       UserService userService = UserServiceFactory.getUserService();

        String thisURL = request.getRequestURI();
        if (request.getUserPrincipal() != null) {
            response.getWriter().println("<p>Hello, " + request.getUserPrincipal().getName() + "!  You can <a href=\"" + userService.createLogoutURL(thisURL) + "\">sign out</a>.</p>");
        } else {
            response.getWriter().println("<p>Please <a href=\"" + userService.createLoginURL(thisURL) + "\">sign in</a>.</p>");
        }

        byte[] oldImageData;  // ...

        ImagesService imagesService = ImagesServiceFactory.getImagesService();

        Image oldImage = ImagesServiceFactory.makeImage(oldImageData);
        Transform resize = ImagesServiceFactory.makeResize(200, 300);

        Image newImage = imagesService.applyTransform(resize, oldImage);

        byte[] newImageData = newImage.getImageData();
        JID jid = new JID("example@gmail.com");
        String msgBody = "Someone has sent you a gift on Example.com. To view: http://example.com/gifts/";
        Message msg = new MessageBuilder().withRecipientJids(jid).withBody(msgBody).build();
                
        boolean messageSent = false;
        XMPPService xmpp = XMPPServiceFactory.getXMPPService();
        if (xmpp.getPresence(jid).isAvailable()) {
            SendResponse status = xmpp.sendMessage(msg);
            messageSent = (status.getStatusMap().get(jid) == SendResponse.Status.SUCCESS);
        }
        Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);

        String msgBody = "...";

        try {
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress("admin@example.com", "Example.com Admin"));
            msg.addRecipient(Message.RecipientType.TO,new InternetAddress("user@example.com", "Mr. User"));
            msg.setSubject("Your Example.com account has been activated");
            msg.setText(msgBody);
            Transport.send(msg);
    
        }
        try {
            URL url = new URL("http://www.example.com/atom.xml");
            BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
            String line;

            while ((line = reader.readLine()) != null) {
                // ...
            }
            reader.close();

        }
        Cache cache;

        try {
            cache = CacheManager.getInstance().getCacheFactory().createCache(Collections.emptyMap());
        } catch (CacheException e) {
            // ...
        }

        String key;      // ...
        byte[] value;    // ...

        // Put the value into the cache.
        cache.put(key, value);

        // Get the value from the cache.
        value = (byte[]) cache.get(key);
 

Loading comments...

Please log in to add your comment.

Report abuse

More presentations by Michal Srajer