Reaching out from PL/SQL (Oracle Open World 2011)

61
REACHING OUT FROM PL/SQL Lucas Jellema – AMIS (Nieuwegein, The Netherlands) Oracle Open World 2011

description

PL/SQL applications do not live on an island. Increasingly, applications need relate to the rest of the world. Either to make themselves and the services they provide accessible to external parties - that may not speak PL/SQL at all - or to access information or enlist help from external services. PL/SQL can do much more than invoke other PL/SQL applications or execute SQL as it provides many inbound and outbound channels for such interactions. This session demonstrates a number of channels and discusses when and why to use them and how to use them, including HTTP and WebServices for RSS, SOAP and REST, Java for JDBC, JMS, Email and Chat. The demos include PL/SQL responding to requests as well as pushing messages out to external consumers.Push to the limit - rich and pro-active user interfaces with ADFVisual appeal and desktop-like reaction to user input were key elements in Web 2.0, powered by AJAX. The next generation user interface is active - presenting data and events in real time, driven by server push technology. This session demonstrates how ADF Active Data Service (ADS) and the BAM Data Control are leveraged to create an active UI. Messages arriving on JMS, through HTTP, from Complex Event Processing and from email servers are pushed to the browser, updating charts, tables and even causing popups to open. The session demonstrates a Chat implementation integrated in a Fusion Web application. It also shows how Database Query Result Change Notification and ADS allow events to be pushed from database all the way to user interface.

Transcript of Reaching out from PL/SQL (Oracle Open World 2011)

Page 1: Reaching out from PL/SQL (Oracle Open World 2011)

REACHING OUT FROM PL/SQL

Lucas Jellema – AMIS (Nieuwegein, The Netherlands)

Oracle Open World 2011

Page 2: Reaching out from PL/SQL (Oracle Open World 2011)

ASK WHAT YOU CAN DO FOR YOUR COUNTRY?!

Do not ask what your country can do for you

Page 3: Reaching out from PL/SQL (Oracle Open World 2011)

AND ALSO ASK WHAT YOU (THROUGH PL/SQL & THE DATABASE) CAN DO FOR YOUR “ENVIRONMENT”?!

By all means, ask what the world around you can mean for your PL/SQL application

Page 4: Reaching out from PL/SQL (Oracle Open World 2011)

Database(SQL & PL/SQL)

Page 5: Reaching out from PL/SQL (Oracle Open World 2011)

Database(SQL & PL/SQL)

SOA

Page 6: Reaching out from PL/SQL (Oracle Open World 2011)

Database(SQL & PL/SQL)

Email

User Interface

Chat (IM)

File (Excel, PDF,…)

RSS

Queue

REST

WebService

File, O/S

JDBC, SQL Net, DB Link

Page 7: Reaching out from PL/SQL (Oracle Open World 2011)

THE HUMAN SIDE OF THINGS

Page 8: Reaching out from PL/SQL (Oracle Open World 2011)

EMP

Page 9: Reaching out from PL/SQL (Oracle Open World 2011)

CHALLENGES

Page 10: Reaching out from PL/SQL (Oracle Open World 2011)

EMP

Page 11: Reaching out from PL/SQL (Oracle Open World 2011)
Page 12: Reaching out from PL/SQL (Oracle Open World 2011)
Page 13: Reaching out from PL/SQL (Oracle Open World 2011)

Q

Page 14: Reaching out from PL/SQL (Oracle Open World 2011)

Mail Server

receiveMailsendEmail

JSP_WRAPPER

Job

EMAILS

retrieve_emails

`

AQ

notify_email_event

Process_email

EMP

Page 15: Reaching out from PL/SQL (Oracle Open World 2011)

JAVA STORED PROCEDURES

• Java Classes can be loaded into Oracle Database as Java Stored Procedures

• The “JSPs” are wrapped with a PL/SQL wrapper– When invoked no different from normal PL/SQL units

• JSPs are in same session and transaction• JSPs are static• Java Stored Procedures can complement

PL/SQL– for example in dealings with O/S, Mail Server,

Application Server, IM Server, TCP/IP, Embedded, Devices

• JMX is exposed by Java-in-Database to monitor

Page 16: Reaching out from PL/SQL (Oracle Open World 2011)

JAVA STORED PROCEDURES – JAVA BEHIND PL/SQL WRAPPER

Page 17: Reaching out from PL/SQL (Oracle Open World 2011)

1• Write Functional Java

2• Wrap with PL/SQL

3• Load into Database

4• Set appropriate Privs

5• Invoke JSP via PL/SQL Interface

Page 18: Reaching out from PL/SQL (Oracle Open World 2011)

THE ULTIMATE OUTREACH…

Page 19: Reaching out from PL/SQL (Oracle Open World 2011)

OTHER SCENARIO WITH DATABASE AS INITIATOR OF THE DIALOG• An Update of an Employee’s Salary takes

place• After Update Row level trigger fires

– creates job to send out an email to have someone approve the salary raise

– Sets the status of the salary to ‘awaiting approval’

• Email is sent to approver• Approvers sends an answer

– Decision and comments

• Answer is received & processed by database– Status of salary record is updated

Page 20: Reaching out from PL/SQL (Oracle Open World 2011)
Page 21: Reaching out from PL/SQL (Oracle Open World 2011)

EJB

JMS

Email

Libraries for:• Graphical Manipulation• Translation, • Calculation,• Encryption,• …

CommunicationProtocols: http, ftp, tcp ip, sms, grid, JDBC, JDBC-ODBC, .Net, …

Operating System& File System

Page 22: Reaching out from PL/SQL (Oracle Open World 2011)

dbms_epg(mod_plsql)

http

XMLDB

httpftp

webdav

utl_mailutl_fileexternal

table

Page 23: Reaching out from PL/SQL (Oracle Open World 2011)

APEX

THE ULTIMATE DATABASE USER INTERFACE

dbms_epg(mod_plsql)

Page 24: Reaching out from PL/SQL (Oracle Open World 2011)

DATABASE REACHING OUT TO HUMAN PARTNERS• DBMS_EPG (or good old mod_plsql)

– Expose HTML and RSS & Process HTTP requests

• XMLDB– Expose and allow manipulation of resources

through HTTP, FTP and WEBDAV

• Stored Java Procedures to– Interact with Mail Server, Chat; leverage – Process human file formats (PDF, Excel, images,

…)

• Supplied packages: UTL_MAIL, UTL_FILE

Page 25: Reaching out from PL/SQL (Oracle Open World 2011)

EXPOSE STORED PROCEDURES VIA HTTP• Stored Procedures and Packages can be

configured to handle HTTP requests– Using the supplied DBMS_EPG package

• They compose the HTTP response by writing strings to the HTP.P procedure– Or return binary documents using wpg_docload

• Database infrastructure handles HTTP communication – including security, headers, get/post parameters

• Embedded PLSQL Gateway – powering– APEX and other PL/SQL based HTML UI– Expose RESTful Web Services– Publish RSS Feeds– Asynchronous HTTP notifications

Page 26: Reaching out from PL/SQL (Oracle Open World 2011)

PUBLISH PACKAGE AS HTTP-BASED API USING DBMS_EPG• Hide database protocol

– Not its physical location nor the schema, and user authentication

• HTTP communication is truly cross technology– Browser, Java, .Net, JavaScript & RIA clients, …

• Is also possibly remote, across networks, firewalls etc. – easier than normal JDBC connections

• Can publish in various formats– Text, HTML, CSV, JSON, XML, RSS

• Use case:cross-technology, internal no WS*/ESB

http

Page 27: Reaching out from PL/SQL (Oracle Open World 2011)

SIMPLE DBMS_EPG EXAMPLE

sys

scott

scott

Page 28: Reaching out from PL/SQL (Oracle Open World 2011)

MACHINATIONS

Page 29: Reaching out from PL/SQL (Oracle Open World 2011)

CALL FOR DECOUPLING

• Minimize dependencies and maximize interoperability and reuse

• Through decoupling:– Encapsulate functionality, legacy and

complexity– Interact through well defined interfaces– Support standard protocols and cross

technology facilities (XML, HTTP, JSON, SQL)– Leverage asynchronous exchange whenever

relevant• Take care of the data(base)

– ‘No SQL’ approach: SQL is encapsulated inside database API (View and Package)

– SQL is not left to application developers or 3rd party frameworks

– Data integrity is enforced inside the database

Page 30: Reaching out from PL/SQL (Oracle Open World 2011)

EXTREME ENCAPSULATION

• View on top of table(collection)– With instead of trigger handling DML

– Data queried from NUMBERS is produced in PL/SQL – no underlying table involved

– Data manipulated in NUMBERS is processed in memory

View Numbers

Package P

Page 31: Reaching out from PL/SQL (Oracle Open World 2011)

EXTREME ENCAPSULATION

Page 32: Reaching out from PL/SQL (Oracle Open World 2011)

Database(SQL & PL/SQL)

EmailChat (IM) RSS

Queue

REST

WebService

File, O/S

JDBC, SQL Net, DB Link

dbms_epg(mod_plsql)

http

XMLDB

httpftp

webdav

utl_mailutl_fileexternal

table

utl_http(httpuritype)

http

JDBC/ODBCOCI, DB Link

utl_tcp

AdvancedQueuing

AQjms

nativedbws

Page 33: Reaching out from PL/SQL (Oracle Open World 2011)

Chat/IM XMPP Server

HTTP

JDBC

Other(Email, FTP/File,

XMPP/Chat)

SOA Suite

Oracle Service

Bus

DB

AQ

JMS

EJB

FileFTP

SDO

WS

http

PL/SQL package

Table

AQ

Native DB WebService

EPG

View

WebLogic Server Database

JAX-WS

ADF BC/SDO WS

EJB/JPA

Email ServerFile/FTP Server

UMS

XMLDB

XMLTypes

XMLXML & XSD

JSON/ CSV

Ref Cursor

Types & CollJPublisher

WS

utl_file, BFILE,

URITYPE

JMS Queue

XMLRelational/Oracle Type

Page 34: Reaching out from PL/SQL (Oracle Open World 2011)

BE A GOOD HOST

• Provide services– Views for easy querying– Stored Procedure based APIs

• For data retrieval and data manipulation

– Queues as way-in (publish) as well as way-out (allow subscription) for events

– Send change notifications• Http based push messages• Query Result Change Notification

Page 35: Reaching out from PL/SQL (Oracle Open World 2011)

THE SUPER HOST

Query Result Change Notification

Page 36: Reaching out from PL/SQL (Oracle Open World 2011)

QUERY RESULT CHANGE NOTIFICATION• Continuous Query Notification:

– Send an event when the result set for a query changes

– Background process calls PL/SQL Handler or Java Listener or OCI client when thecommit has occurred

– Event contains rowidof changed rows

• Used for:– Refreshing specific

data caches (middletier, global context)

– (custom) Replication

PL/SQL

Java Listener

Page 37: Reaching out from PL/SQL (Oracle Open World 2011)

DATABASE TO MIDDLE TIER TO CLIENT NOTIFICATION THROUGH HTTP

Application Scope event handler

HTTP

Proce-dure

UTL_HTTP

servlet

Session cache

EMP

servlet1

2

3 45

6

7

8

9A

D

B

C

Job

Page 38: Reaching out from PL/SQL (Oracle Open World 2011)

CROSS TIER PUSH

Web Browser

RDBMS

JEE Application Server

trg

job

servletevt lsnr

Page 39: Reaching out from PL/SQL (Oracle Open World 2011)

MORE INTERACTION FROM & TO THE DATABASE• Files into the database• Database to leverage O/S• Stored Java for non-human interactions• WebServices – publish and consume• Engage Middle Tier as ‘agent’

Page 40: Reaching out from PL/SQL (Oracle Open World 2011)

THE BROWSER IN YOUR DATABASEINTRODUCING UTL_HTTP

≈select utl_http.request ( 'http://technology.amis.nl/blog/?feed=rss2' , null )from dual

Page 41: Reaching out from PL/SQL (Oracle Open World 2011)

THE DATABASE CAN ASK THE MIDDLE TIER TO ACCESS AND PROVIDE SERVICES

• Get information from (or to) services on the intranet or internet

• Publish/Send information to the internet (RSS, email, chat)

• Can tell the middle tier– Interesting events – Alert about

(im)pending issues, attempted rule violations, …

– The outcome of batch calculations

• DB vs. Middle Tier:Security, Functionality, Load Balance

WebLogic Server 11g

SOA Suite 11g

AQ

Web App

Page 42: Reaching out from PL/SQL (Oracle Open World 2011)

HAPPY CHATTING – IM FROM THE DATABASE• Database can call a middle

tier service using utl_httpor HTTPURITYPE– Passing in the destination

and the message content• This Service sends it

onwards to the Google Talk Server WebLogic Server 11g

Service

CHAT_MGR

Service

Page 43: Reaching out from PL/SQL (Oracle Open World 2011)

DATABASE ACTIVELY INFORMS ON EVENTS

WebLogic Server 11g

Service

CHAT_MGR

Service

EMP

Page 44: Reaching out from PL/SQL (Oracle Open World 2011)

CHAT MANAGER PACKAGEcreate or replace package body chat_mgras

c_im_destination CONSTANT varchar2(100):= 'im_account'; c_im_service_base_url CONSTANT varchar2(100):= 'http://host/service?'; c_amp constant varchar2(5) := chr(38);

procedure send_im_message( p_msg in varchar2) is l_response varchar2(32000);begin l_response:= utl_http.request ( c_im_service_base_url ||'imDestination='||c_im_destination ||c_amp||'msgBody='||replace(p_msg,' ', '%20') , null );end send_im_message;

end chat_mgr;

Page 45: Reaching out from PL/SQL (Oracle Open World 2011)

EMP INFORMERcreate or replace trigger mananager_sal_update_watcherbefore insert or update of salon empfor each rowwhen (new.job ='MANAGER' and new.sal > 4000 and new.sal > nvl(old.sal,0))declare l_msg varchar2(32000);begin l_msg:= 'Selfenrichment ' ||'Salary of '||:new.ename ||' ( manager in Department '||:new.deptno||') ' ||'is changed to '||:new.sal ; chat_mgr.send_im_message ( p_msg => l_msg );end;

Page 46: Reaching out from PL/SQL (Oracle Open World 2011)

THE TALKING DATABASE

Details on the Employee. Employee name is Smith, his job is Analyst. He

works in department 20…

EMP

Page 47: Reaching out from PL/SQL (Oracle Open World 2011)

THE TALKING DATABASE

PL/SQLexposed through dbms_epg

httpaudio/mpeg, binary data

Page 48: Reaching out from PL/SQL (Oracle Open World 2011)
Page 49: Reaching out from PL/SQL (Oracle Open World 2011)
Page 50: Reaching out from PL/SQL (Oracle Open World 2011)

RESTFUL WEBSERVICES

• Simple, stateless, light weight, http-based message exchange protocol– Use standard get, put, post, delete operations– Use URL to pass resource reference

• Small, easy-to-understand messages• Typically XML or JSON based• Not using SOAP, or the WS*-stack• Often used directly from User Interface –

(AJAX enabled) JavaScript– RESTful services are primary communication

mechanism for Mobile Apps

Page 51: Reaching out from PL/SQL (Oracle Open World 2011)

RESTFUL ARCHITECTURE

RESTful PL/SQL APIexposed through dbms_epg

httphttp

Page 52: Reaching out from PL/SQL (Oracle Open World 2011)

RESTFUL RESOURCE NAVIGATION

Page 53: Reaching out from PL/SQL (Oracle Open World 2011)

PUBLISH REST-FUL HTTP API – DIRECTLY ON TOP OF THE RDBMS

Page 54: Reaching out from PL/SQL (Oracle Open World 2011)

PUBLISH REST-FUL HTTP API – DIRECTLY ON TOP OF THE RDBMS

Page 55: Reaching out from PL/SQL (Oracle Open World 2011)

CONSUMING REST-FUL SERVICES

• Invoking a REST-ful service is simply making an HTTP request to some URL

• Can be done through:– utl_http– httpuritype– Stored Java

• Result can be processed as just a string or as XML, JSON or other format– OSS Libraries can help with special formats, for

example PL/JSON for JSON

Page 56: Reaching out from PL/SQL (Oracle Open World 2011)

SOAP WEBSERVICES

• All messages (input and output) are XML• The message consists of two parts inside an

envelope (a SOAP XML wrapper)– The header with meta-data– The body with the contents to be handled by or

returned by the service• The WebService Definition Language (WSDL)

document describes the service• An XML Schema Document (XSD) describes

the structure of the XML messages– XSD is like an ERD or Table Design

Page 57: Reaching out from PL/SQL (Oracle Open World 2011)

CALLING ‘FORMAL’ WS* ANDSOAP BASED WEB SERVICES• Invoking a SOAP WS* service is making an

HTTP request to some URL• Can be done through:

– utl_http– Stored Java– utl_dbws

• Request and Responseare both SOAP-wrapped XML messages– Using XMLDB

functionality including XML Type these can easily by composed, transformed, validated and parsed

Page 58: Reaching out from PL/SQL (Oracle Open World 2011)

ORACLE RDBMS 11G – NATIVE DATABASE WEBSERVICES• Database Schema can be published through

native database web services– Each package corresponds with a WSDL

• With an operation for each Procedure or Function• Auto-mapping from PL/SQL input and output

parameters to WSDL (XSD) Types

– WSDL and XSD are dynamically generated– A generic Query Service (across schema) is

exposed• To execute user defined queries• With support for pagination and some formatting

– http and https is supported– Limited control over WSDL & XSD

– Use case: internal, cross technology, WS enabled client, no ESB or Application Server available

Page 59: Reaching out from PL/SQL (Oracle Open World 2011)

SUMMARY

Page 60: Reaching out from PL/SQL (Oracle Open World 2011)

SESSION PLUGGING

• Sunday – Gold Nuggets in ADF (S32502)3.15-4.15, Moscone West-2000

• Tuesday – Reaching Out from PL/SQL (S08625 )10.15-11.15, Mariott Marquis, Salon 7

• Tuesday – (JavaOne) Cross Tier Push Architecture (Don’t call us, we’ll push you) (S24722)4.30-5.15 PM, Hotel Nikko, Monterey I/II

• Wednesday – Push to the Limit: Rich and Proactive User Interfaces with Oracle ADF (S08620)5-6 PM, Mariott Marquis, Golden Gate A

• Thursday – Instant Agility in Oracle Fusion Middleware through Design Time @ Run Time (S08623)4-5 PM, Moscone West – 2020

Page 61: Reaching out from PL/SQL (Oracle Open World 2011)