Using Socket Adapter in Oracle Service Bus

Tweet about this on TwitterShare on Facebook0Share on LinkedIn0Share on Google+0

A few days ago, I helped a customer to integrate an IBM mainframe and Oracle Service Bus using Socket Adapter. I know I could use the JCA Adapter for Mainframe or Oracle Tuxedo, which are better in this type of integration, but the client did not have time to buy these solutions.

In this post, you will know how to use Socket Adapter to communicate using text messages, and you may download the sample application: OSBSocketAdapterApp.zip.

Create a new Service Bus Application.

using-socket-adapter-in-oracle-service-bus1

Create the TXT file that we will use to create the NXSD Schema.
In the Applications window, right-click the Project Name and choose New > From Gallery.
In the New Gallery dialog, choose General > File, and click OK.
Set the File Name as employee.txt and click OK.

using-socket-adapter-in-oracle-service-bus2

Create the Request XSD File.
In the Applications window, right-click the Project Name and choose New > From Gallery.
In the New Gallery dialog, choose Service Bus Tier > Interfaces > NXSD Schema, and click OK.
Set the File Name as createEmployeeRequest.xsd and click Next.

using-socket-adapter-in-oracle-service-bus3

Click Next.

using-socket-adapter-in-oracle-service-bus4

Choose the employee.txt file and click Next.

using-socket-adapter-in-oracle-service-bus5

Click Next.

using-socket-adapter-in-oracle-service-bus6

Set the Target Namespace and Element Names, and click OK.

using-socket-adapter-in-oracle-service-bus7

Click Next.

using-socket-adapter-in-oracle-service-bus8

We will send a header with the message, so check “Use the first record as the field names” option and click Next.

using-socket-adapter-in-oracle-service-bus9

Click Next and than click Finish.

using-socket-adapter-in-oracle-service-bus10

Create the Response XSD File.
In the Applications window, right-click the Project Name and choose New > From Gallery.
In the New Gallery dialog, choose Service Bus Tier > Interfaces > XML Schema, and click OK.
Set the File Name and Target Namespace, and click OK.

using-socket-adapter-in-oracle-service-bus11

Paste the following code inside the XSD file:

<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns="http://waslley.souza.com.br/Employee"
        targetNamespace="http://waslley.souza.com.br/Employee"
        elementFormDefault="qualified">
  <xsd:element name="Response">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="Message" type="xsd:string"/>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

using-socket-adapter-in-oracle-service-bus12

Open the SocketAdapterProject file.
Right-click in Proxy Service area and choose Insert Adapters > Socket.

using-socket-adapter-in-oracle-service-bus13

Click Next.

using-socket-adapter-in-oracle-service-bus14

Click Next.

using-socket-adapter-in-oracle-service-bus15

Select the first option and click Next.

using-socket-adapter-in-oracle-service-bus16

Click Next.

using-socket-adapter-in-oracle-service-bus17

Choose the XSD files and click Next.

using-socket-adapter-in-oracle-service-bus18

Choose the “Use XSLT to define the handshake” option, click the plus green icon to create Request and Response XSLT files and click Finish.

using-socket-adapter-in-oracle-service-bus19

Drag the arrow from socketService and drop within the pipeline area.

using-socket-adapter-in-oracle-service-bus20

Click Next.

using-socket-adapter-in-oracle-service-bus21

Click Finish.

using-socket-adapter-in-oracle-service-bus22

Double-click socketServicePipeline and configure it like the image below.

using-socket-adapter-in-oracle-service-bus23

Paste the following code inside the value property of Replace component:

<emp1:Response>
  <emp1:Message>
    {fn:concat(count($body/emp:Employees/emp:Employee)," message(s) received!")}
  </emp1:Message>
</emp1:Response>

using-socket-adapter-in-oracle-service-bus24

Paste the following code inside the request.xsl file:

<xsl:template match="/" xmlns:socket="http://www.oracle.com/XSL/Transform/java/oracle.tip.adapter.socket.ProtocolTranslator">
  <xsl:copy-of select="socket:socketReadWithXlation()" />
</xsl:template>

using-socket-adapter-in-oracle-service-bus25

Paste the following code inside the reply.xsl file:

<xsl:template match="/" xmlns:socket="http://www.oracle.com/XSL/Transform/java/oracle.tip.adapter.socket.ProtocolTranslator">
  <xsl:variable name="temp">
    <xsl:value-of select="/ns0:Response/ns0:Message"/>
  </xsl:variable>
  <xsl:variable name="var1" select="socket:socketWrite($temp, '','')"/>
  <xsl:variable name="var2" select="socket:socketEndOutput()"/>
</xsl:template>

using-socket-adapter-in-oracle-service-bus26

Deploy your project in Service Bus Server!
In WebLogic Server Administration Console, go to Deployments and click SocketAdapter.
Go to Configuration tab and then Outbound Connection Pools.
Click eis/socket/SocketAdapter and change KeepAlive property to true.

using-socket-adapter-in-oracle-service-bus27

Click Save.
Go to Deployments and update the SocketAdapter.
That’s all! Create a new Java Class with the following code to test your application.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

public class SocketClient {
  private static final String HOST = "localhost";
  private static final int PORT = 12110;
  private static final String MESSAGE = "FirstName,LastName,Job\nWaslley,Souza,Sales Consultant\nJohn,Snow,Security";
    
  public static void main(String[] args) {
    try {
      Socket socket = new Socket(HOST, PORT);
      OutputStream os = socket.getOutputStream();
      os.write(MESSAGE.getBytes());
      os.flush();
      socket.shutdownOutput();
      BufferedReader soc_in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
      String response = soc_in.readLine();
      System.out.println("Response: " + response);
      socket.close();

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

using-socket-adapter-in-oracle-service-bus28

You may use log component to validate the message.

using-socket-adapter-in-oracle-service-bus29

Tweet about this on TwitterShare on Facebook0Share on LinkedIn0Share on Google+0

Autor: Waslley Souza

Consultor Oracle com foco em tecnologias Oracle Fusion Middleware e SOA. Certificado Oracle WebCenter Portal, Oracle ADF e Java.

Deixe uma resposta

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *