Anda di halaman 1dari 80

Java SE 6 and 7 Features


Carol McDonald
–JavaArchitect
●Sun Microsystems

1
Java SE Platform Roadmap and
Themes Multiple Languages
Modularity
Rich Clients
Java SE 6 Java SE 7

Announce JavaFX
JavaFX SDK

2006 2007 2008


2009

JRE 6u10
'Consumer JRE'
Open Sourcing OpenJDK™
Java Launch JRE 6u5p
'It’s not a matter of 'Performance JRE'
when but how'

2
Agenda
Java SE Technology Today
Dynamic Language Integration
Web Services Enhancement
Database Update
Desktop Improvement
Java SE 6 Update 10
New Developments in the Platform
Multiple Languages
Modularity
Management and VisualVM
Rich Clients

3
Project OpenJDK
Open Source Java SE Platform Implementation


OpenJDK is an open source project that implements Java SE 6
(and beyond) platform

Majority of the code released under GPLv2
• OpenJDK community
> openjdk.java.net

4
How Java SE Platform Gets
Developed
Present and future

What API Specifications JDK Implementation

Where jcp.org openjdk.java.net

How Expert Groups OpenJDK projects

When Now Now!

5
JDK™ version 6 – Filling in the distribution gaps

• OpenJDK-based Java SE 6
+ bundled in Ubuntu 8.04
TS-5214
OpenJDK Community Update
Wed 2.50pm

• Apple releases Java SE 6 for 64bit


OS-X

6
Java SE 6 Platform: Top 10 Features

Scripting Ability to mix JavaScript™technology with Java code


Web Services Easy to use Web Service APIs
Database Updated JDBC APIs, all-Java database in JDK

Desktop AWT/Swing API enhancements, Consumer JRE

Monitoring and Management JDK Tools


Compiler Access APIs to control the compiler
Pluggable Annotations Define your own annotation processors
Java SE For Deployment Consumer JRE
Security Further support for security APIs
Performance Performance, performance, performance

7
Java SE 6 : Top 10 Features

• Web Service APIs


• Scripting language support
• Updated database APIs
• Swing enhancements
• Monitoring and Management
• Compiler APIs
• Pluggable Annotations
• Desktop deployment
• Integration with native security
services
• Quality, compatibility and stability 8
Scripting
Languages on the VM: Past

10
Languages on the JVM™: Present Sleep

Bex Script WebL Funnel Tea iScript


Zigzag JESS Jickle
Modula-2 Lisp
Simkin
Nice
CAL JavaScript
JudoScript
Correlate
Simkin Drools
Basic
Groovy Eiffel Luck
Icon v-language Pascal
Prolog
Tcl
Mini
PLAN Hojo Scala
Rexx JavaFX Script
FScript
Tiger Anvil Yassl foo Oberon
E Smalltalk
Logo
Tiger
JHCR JRuby
Ada G Clojure Scheme Sather
Processing Dawn Phobos
TermWare
LLP Pnuts PAN-5435
BeanShell Forth PHP Script Bowl
C# Wed 9.30am
SALSA ObjectScript
Yoix
Jython Piccola
11
Java Platform Supports Scripting
• Java Language != Java Platform
> Java VM runs “language-neutral” bytecode
• Class files written in other languages
> Groovy
> Jython Compiler
> Jruby
• Why?
> Many other languages, many other virtues
> Rapid prototyping and experimentation
> different types of developers

12
Scripting
• Scripting for the Java Platform (JSR 223)
> Framework APIs for adding script engines
> Access to resources of the Java platform, JAR files, from
scripting languages
> Developer APIs to mix script fragments with Java
language code
> Access to scripting languages from Java
• A JavaScript engine is included in JDK 6
> Mozilla Rhino engine
• other JSR 223 compliant scripting engines (Groovy,
Ruby, Python, et al) can be downloaded from
> scripting.dev.java.net
13
Launch Script within Java Code

// create a ScriptEngineManager
ScriptEngineManager m = new ScriptEngineManager();

// get an instance of JavaScript script engine


ScriptEngine engine = m.getEngineByName("js");

// evaluate a script
engine.eval("alert(\"Hello World!\")");

14
Bind Java Objects to script
// create script engine manager
ScriptEngineManager manager = new ScriptEngineManager();

// create JavaScript engine


ScriptEngine engine = manager.getEngineByName(“JavaScript”);
File f = new File(“test.txt”);

// expose File object as variable to script


engine.put(“file”, f);

// evaluate a script string


// script accesses “file” variable and calls method on it
engine.eval(“print(file.getAbsolutePath())”);

Java Script Java object

15
Scripting on Java SE 6 Platform
// create a ScriptEngineManager & grab an engine
ScriptEngineManager m = new ScriptEngineManager();
ScriptEngine engine = m.getEngineByName("js");

// evaluate javascript directly....


jsEngine.eval("print('Hello, world!')");
// ...or from a script
InputStream is =
this.getClass().getResourceAsStream("/scripts/hello.js");
Reader reader = new InputStreamReader(is);
engine.eval(reader);
// ...even call functions and methods in a script
engine.eval("function sayHello() {" +
" println('Hello, world!');" +
"}");
Invocable invocableEngine = (Invocable) jsEngine;
invocableEngine.invokeFunction("sayHello");

16
Demo: Scripting with Java SE
• Build and run ScriptPad sample app from JDK 6
samples
> You can build and run as NetBeans project
• Executing JavaScript code
• Invoking Java methods from JavaScript code

17
Web Services
XML and Web Services
• JAX-WS is part of Java SE 6 platform
> Interoperability: Standards
> SOAP 1.2
> WS-I Basic Profile 1.1
• Data binding using JAXB 2.0
> XML <=> Java Object
• Updates to the JAXP, which includes StaX
> Xml parsing “Pull model”

19
Web Service – Example
// Create a POJO

public class Calculator {


public int add(int a, int b) {
return a+b;
}
}

20
Web Service – Example
// Create a service implementation
@WebService
public class Calculator {
public int add(int a, int b) {
return a+b;
}
}

// Create and publish the endpoint


Calculator calculator = new Calculator();
Endpoint endpoint = Endpoint.publish
(“http://localhost/calculator”,calculator);

21
Web Service Development
• Tools to generate necessary files for you
> wsgen: generates service stubs on the service side
e.g.
wsgen -cp . example.Stock -wsdl -servicename
{http://mynamespace}MyService
> wsimport: generates proxy stub on the client side
e.g.
wsimport -d generated http://example.org/stock?wsdl

22
Web Service Client

// Create a Service object


CalculatorService svc =
new CalculatorService();

// Create a proxy from the Service object


Calculator proxy =
svc.getCalculatorPort();

// Invoke a Web service operation


int answer = proxy.add(35, 7);

23
Database
Java DB
• Java DB database bundled with JDK 6
> Java DB is based on Apache Derby
• open source Apache Derby 100% Java database
• Ideal for embedded or client-server use.
> Small -- 2MB.
> Embed in browser for better Web 2.0.
> Easy application + DB distribution.

25
JDBC 4.0 SQL XML support
• SQLXML Data Type:
SQLXML sqlxml = resultSet.getSQLXML(column);
InputStream binaryStream = sqlxml.getBinaryStream();
Document result = parser.parse(binaryStream);
• Support for WebRowSet
> Extends CachedRowSet + Rows in XML
ResultSet rs = stmt.executeQuery("select * from student");
WebRowSet wrs = new WebRowSetImpl();
wrs.populate(rs);
wrs.writeXml(new FileOutputStream("student.xml"));

26
JDBC 4.0 Annotation-Based SQL
Queries
public class Person {
public String id;
public String firstName, String lastName;
public String deptCode;
}

public interface PersonQueries


extends BaseQuery {
// Get everyone from the table
@Select(“select * from emp_table”)
public Dataset<Person> getEveryone( );

public Dataset<Person> getEveryone(String code);


// Delete a particular person
@Update(“delete emp_table where id = {personId}”)
public int deleteEmployee(String personId);

27
Desktop APIs
Improvements
Desktop APIs Improvements
• AWT improvements
> Tray icon
> Splash screen
> Desktop class
> Dialog modality enhancements and API
> Text printing
• Swing improvements
> GroupLayout – basis for NetBeans GUI Builder
(Matisse)
> JTable sorting and filtering
> SwingWorker
29
Tray Icon Support
• Tooltip and Image
• Popup menu
> Actions, events respond to clicks
// Construct a TrayIcon
TrayIcon trayIcon = new TrayIcon(image, "Tray Demo",
popupMenu);

// Set the TrayIcon properties


trayIcon.addActionListener(actionListener);

// Add the tray icon


SystemTray.getSystemTray().add(trayIcon);

30
Splash Screen Support

• Displays a splash screen instantly – before the


JVM™ starts!
> Display from command line
java -splash:image.gif TheApp

31
Java Desktop integration
• java.awt.Desktop API
> Launch the default browser
> Launch the default email client
> Launch the default application to handle file open, file edit
and file print
Desktop desktop = Desktop.getDesktop();

if (desktop.isSupported(Desktop.Action.EDIT))
{ desktop.edit(file }

desktop.browse(url);

32
SwingWorker
For Easy Multi-threaded Application Development
With Swing
• Makes use of concurrency package
• Makes it easy to offload work to separate threads
• Example :
> http://java.sun.com/developer/technicalArticles/javase/swingworker
• More Information:
http://java.sun.com/docs/books/tutorial/uiswing/concurrency/index.html#SwingWorker

33
SwingWorker – Example
class ImageRetriever extends SwingWorker<Icon, Void> {

@Override
public String doInBackground() {
Icon icon = retrieveImage(strImageUrl);
return icon;
}

@Override
protected void done() {
lblImage.setIcon(icon);
..
}
}
// in event handler tell thread to begin asynchronous method.
ImageRetriever imgRetriever =
new ImageRetriever(lblImage, imageUrl);

imgRetriever.execute();
// This event thread continues here without blocking.
34
Monitoring &
Management
jconsole, jps, jmap, jhat, jstack
• Run a sample Java application
• Use the tools
> Use jps to see process ids of all Java processes
> Use jconsole to dynamically connect to a process
> Use jmap to capture snapshot of memory heap of a Java
process
> Use jhat to interpret heap
> Use jstack to thread-dump on a live process
• You can try this
> www.javapassion.com/handsonlabs/javase6tools/

36
Compiler Access
Compiler Access

• programmatic access to javac for in-process


compilation of dynamically generated Java code
• aimed at java tool developers
> JavaServer Pages (JSP) or PHP construction kit
engines that need to generate classes on demand
> developers benefit from faster tools
> Jasper JSP engine runs JSP TCK 3.5x faster

38
Pluggable Annotations
Pluggable Annotations

• Allow developers to define new annotations...


@ForReview
public void myMethod() {...}

• ...and APIs to define components that process them...


import javax.annotation.processing.*;

public class ForReviewProcessor extends AbstractProcessor {..}

• ...and integrate them with the Java Compiler


javac -processor ForReviewProcessor MyCode.java

40
Security
Security

• Added new APIs


> XML Digital Signature (XMLDSig) API (JSR 105)
> Smart Card I/O API (JSR 268)
• Improved authentication schemes
> JAAS-based authentication using LDAP

42
Java SE 6 Update 10
Java SE 6u10 “Consumer JRE”

• Updated Java Runtime environment for the desktop


• Recharge use of Java for rich client applications
> Small, fast download (Java Kernel)
> Unified Java applet / application deployment ith JNLP
> Quick applet startup time (Java Quick Starter)
> JavaFX ready
• Get the beta
> jdk6.dev.java.net/6u10ea.html

44
Java SE 6u10 “Consumer JRE”

• Quickstarter
> Reduces the start up time for Java applications and applets
• Java Kernel
> divides JRE libraries into small bundles. Only download whats
needed now, rest later
• Deployment Toolkit
> Enables easy detection and installation of the JRE
• New Improved Java Plug-In
• Nimbus:cross-platform Swing look & feel
45
Old Java Plugin Architecture

• Applets in browser process


> Same VM
> Same lifecycle
> Same resources
> Different deployment from
Applet Applet Java Applications
Applet
JVM
in
Browser

46
New Java Plugin Architecture
• Applets have own process
> Choose the VM they want
> Live outside browser
> Live beyond the browser
> Same deployment as Java
Applications

JVM
in
Browser

Applet Applet

JVM

Applet
JVM
47
Nimbus Look and Feel Improvements
SwingSet

48
Performance
Java SE 6u5p “Performance Release”

• Performance tuned release of the


JRE™
• 64bit architectures only
> Solaris, Windows, Linux
• Large number of optimizations:
> Crypto libraries, TreeMap, HashMap, XML
Parsing
> Escape analysis, depth-first copying, page
sizes
• Optimizations seep into future JRE
updates
50
Java Platform Performance Trends on x64
SPECjbb2005 onSun x64 Server
220%
210%
200%
190%
% Perf. Improvement

180%
170%
160%
150%
140%
130%
120%
110%
100%
J2SE J2SE J2SE J2SE Java SE 6.0 Java SE Java SE Java SE
5.0_04 5.0_05 5.0_06 5.0_08 6.0_02 6.0_05 6.0_05-P

51
Java Platform Performance Trends on x64
SPECjbb2005 onIntel and AMD
280%

260%

240%
% Perf. Improvement

220%

200%
Intel Core 2
180% AMD Barcelona

160%

140%

120%

100%
J2SE J2SE J2SE J2SE Java SE 6.0 Java SE Java SE Java SE
5.0_04 5.0_05 5.0_06 5.0_08 6.0_02 6.0_05 6.0_05-P

52
Java SE 7 Highlights:
Scripting
Languages on the JVM™: Present
Sleep

Bex Script WebL Funnel Tea iScript


Zigzag JESS Jickle
Modula-2 Lisp
Simkin
Nice
CAL JavaScript
JudoScript
Correlate
Simkin Drools
Basic
Groovy Eiffel Luck
Icon v-language Pascal
Prolog
Tcl
Mini
PLAN Hojo Scala
Rexx JavaFX Script
FScript
Tiger Anvil Yassl foo Oberon
E Smalltalk
Logo
Tiger
JHCR JRuby
Ada G Clojure Scheme Sather
Processing Dawn Phobos
TermWare
LLP Pnuts PAN-5435
BeanShell Forth PHP Script Bowl
C# Wed 9.30am
SALSA ObjectScript
Yoix
Jython Piccola
54
Languages on the JVM: Future
Java SE 7

• Broadening the (J)VM to accelerate runtimes


• Bytecode for dynamic invocation
> Anonymous classloading JVM Language Summit
http://jvmlangsummit.com
> Lightweight method handles Sept 2008

> Optimized dynamic invocation


> Continuations and tail call optimizations
• DaVinci Project: openjdk.java.net/projects/mlvm
> Draft specification
> Prototype available
• JRuby is the pioneer
Plan to bundle other engines into JDK 7
55
Java SE 7 Highlights:
Modularity
Modular programming in Java today
• Packages
> Package names are hierarchical
> Package membership is not hierarchical
• Access control
> Types and members shared across packages must be
made public
• Interfaces
> Not always desirable to have all members be public

57
Java Module System
Classloaders
• Address "JAR hell"
• Better distribution format than JAR files
• Enable side-by-side versioning Versioning
• Simple and predictable classloading

Deployment goals for the Java Module


System

• Support entire spectrum of Java programs Dependency


control
• Repository infrastructure to isolate modules
• Customizable for different environments
(e.g. applets) and module systems Distribution
format
58
Classes in different packages need to
collaborate
org.netbeans.coreis a conceptual"module"
org/
netbeans/
core/
Debugger.class
...
utils/
ErrorTracker.class
...
wizards/
JavaFXApp.class
...
addins/
...

59
Modules in the Java language
// org/netbeans/core/Debugger.java
module org.netbeans.core;
Module concept
package org.netbeans.core;
in the language
public class Debugger {
... new ErrorTracker() ...
}

60
Modules in the Java language
// org/netbeans/core/Debugger.java
module org.netbeans.core;
Module concept package org.netbeans.core;
in the language public class Debugger {
... new ErrorTracker() ...
One module }
has many
packages // org/netbeans/core/utils/ErrorTracker.java
module org.netbeans.core;
package org.netbeans.core.utils;
Module access
specified in the module class ErrorTracker {
language module int getErrorLine() { ... }
}

61
The JAM Module System
• Java Modules
> JAR-like file format
> Easy metadata
> Addresses problems with JARs

62
JAM metadata
Version // org/netbeans/core/module-info.java
@Version("7.0")
Main class @MainClass("org.netbeans.core.Main")
@Attribute(name="IDE", value="NetBeans")
Attributes

Module @ImportModules{
dependencies @ImportModule(name="java.se.core",
version="1.7+"),
Custom import @ImportModule(name="org.foo.util",
policy
version="1.0", reexport=true)
})
Initializer
@ImportPolicyClass("org.netbeans.CustomPolicy")
Platform @ModuleInitializerClass("org.netbeans.core.Init")
specific
@PlatformBinding(os="solaris", arch="sparc")
Exported
@ExportResources({"icons/**"})
resources
module org.netbeans.core;

63
Compiling and running a JAM module
• javac org/netbeans/core/*
• javac org/netbeans/core/utils/*

• jam cvf org.netbeans.core-7.0.jam


org/netbeans/core/*
org/netbeans/core/utils/*

• java -jam org.netbeans.core-7.0.jam

• cp org.netbeans.core-7.0.jam /netbeans/repository
• java -module org.netbeans.core:6.0+
-repository /netbeans/repository

64
org.netbeans.core-7.0.jam

/META-INF/MANIFEST.MF Module metadata


/MODULE-INF/MODULE.METADATA
/MODULE-INF/bin/xml-windows.dll Native libraries
/MODULE-INF/bin/xml-linux.so
JAR files
/MODULE-INF/lib/xml-parser.jar
/org/netbeans/core/module-info.class
/org/netbeans/core/Main.class
/org/netbeans/core/Debugger.class
/org/netbeans/core/utils/ErrorTracker.class
/icons/graphics.jpg
Other resources

• .jam.pack.gz extension for Pack200-gzipped JAM module


65
No more JAR hell
• Can import other JAM module
• Different versions of the "same" module definition (B) can co-exist
> use the highest

• A single version of a module definition (E) can be shared between modules


• A JAM module instance has its own classloader
• Classloading is completely deterministic
Module A v1.0

imports imports imports

Module B v2.0 Module C v3.0 Module D v4.0

imports imports imports

Module B v1.0 Module E v5.0


imports

66
Loading OSGi bundles in JDK version 7
JVM
Bootstrap repository
OSGi Interoperability
Extension repository

JAM repository Repository for


OSGi
OSGi
Container
Java Modules

OSGi bundles

67
Java Module System - Status

• JSR 277 Draft 2 is in progress


> OSGi Interoperability spec is out !

• OpenJDK™ Modules project


> openjdk.java.net/projects/modules/

68
Modularizing Java SE itself

• Java SE has grown very large


• 2 main classes of applications
> Server applications
> Desktop applications
• Introducing Java SE Profiles in Java SE 7
> A formal subset of Java SE, defined in the JCP
> Plus optional packages not part of Java SE
> Formal == Reference Implementation and Test Suite

69
The (full) Java SE Platform

Java SE Applications

Web
Enterprise Core
Services / GUI
APIs APIs
data access
Tools
APIs

JVM

OS

70
Java SE Rich Client Profile

Rich Client
Applications
Enterprise
APIs

Web
Core
Services / GUI
APIs
data access
Tools
APIs
JVM

OS

71
Management Updates
• JSR 255: Java Management Extensions (JMX™) Specification,
version 2.0
> Federated JMX technology servers
> Get code: opendmk.dev.java.net

72
Management Updates
• Web Services Connector for JMX Agents
> early access Get code: ws-jmx-connector.dev.java.net
• Based on web service management standards
> WS-Management (DMTF)
> http://www.dmtf.org/standards/wsman/

WS-Management Request
Client JSR 262 Server
Request MBean Server

Response

WS-Management Response

73
VisualVM for Sun's JDK
• Get the release candidate today BOF-5223
VisualVM
> visualvm.dev.java.net Thu 7.30pm

James Gosling
Extreme Innovation
Fri 8.30AM

74
What's VisualVM?
A new Integrated and Extensible Troubleshooting
Tool for the Java Platform
JDK Management, Monitoring and Troubleshooting
tools and lightweight CPU and Memory profiling
Production and development time tool
Audience: developers, administrators, performance
and sustaining engineers, etc.
https://visualvm.dev.java.net

75
Closure
• anonymous function, for cross cutting concerns
• Closure literals
{ parameters => statements expression }
{ int x, int y => (x + y) }
• Function example
int answer = { int x, int y =>
(x + y) }.invoke(5, 7);

• Evolving specification at
> http://www.javac.info

76
Closure Conversion Example
ActionListener
listener
=

{
ActionEvent
evt
=>
saveAll();
};

JButton
button
=
new
JButton(“OK”);
button.addActionListener(listener);

• Can assign a closure to a variable of compatible


interface type

77
Important Updates
• More New I/O APIs
> New filesystem API
TS-5686
> Revised socket channel functionality New I/O APIs
Thu 9.30am
> Asynchronous I/O

• Draft stage
> openjdk.java.net/projects/nio/
> Spec, code, samples

78
Call For Action
• Java SE 6 platform
> Use it now!
• OpenJDK
> Join us in building it!
• Java SE 7 platform
> Join us in defining it!
• Interact with JDK developers @ planetjdk.org

79
Java SE 6 and 7 Features


Carol McDonald
–JavaArchitect
●Sun Microsystems

80