Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can the Oracle database send requests to a Servlet?

Usually we are sending requests from the browser, however I'm wondering if a database can also do the same thing. Let say I have a servlet on my server and an Oracle database.

Is it possible that by using the Schedule option, the database will be able to send requests to the servlet?

like image 998
Steven James Avatar asked May 14 '18 10:05

Steven James


Video Answer


1 Answers

Using UTL_HTTP

Yes, you can send HTTP requests from the Oracle database. Here's a nice blog post that summarises how you can do it using the UTL_HTTP package: https://oracle-base.com/articles/misc/utl_http-and-ssl

An example from the Oracle manual:

SET SERVEROUTPUT ON SIZE 40000

DECLARE
  req   UTL_HTTP.REQ;
  resp  UTL_HTTP.RESP;
  value VARCHAR2(1024);
BEGIN
  UTL_HTTP.SET_PROXY('proxy.my-company.com', 'corp.my-company.com');
  req := UTL_HTTP.BEGIN_REQUEST('http://www-hr.corp.my-company.com');
  UTL_HTTP.SET_HEADER(req, 'User-Agent', 'Mozilla/4.0');
  resp := UTL_HTTP.GET_RESPONSE(req);
  LOOP
    UTL_HTTP.READ_LINE(resp, value, TRUE);
    DBMS_OUTPUT.PUT_LINE(value);
  END LOOP;
  UTL_HTTP.END_RESPONSE(resp);
EXCEPTION
  WHEN UTL_HTTP.END_OF_BODY THEN
    UTL_HTTP.END_RESPONSE(resp);
END;

Alternative using Oracle AQ

If you want some intermediary layer, you might also use Oracle AQ, which I personally find more powerful: https://docs.oracle.com/database/121/ADQUE/aq_intro.htm

Using Oracle AQ, you could for instance bypass the HTTP layer and access whatever the Servlet is calling internally, directly.

like image 149
Lukas Eder Avatar answered Oct 19 '22 01:10

Lukas Eder