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.
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.
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.
Click Next.
Choose the employee.txt file and click Next.
Click Next.
Set the Target Namespace and Element Names, and click OK.
Click Next.
We will send a header with the message, so check “Use the first record as the field names” option and click Next.
Click Next and than click Finish.
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.
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>
Open the SocketAdapterProject file.
Right-click in Proxy Service area and choose Insert Adapters > Socket.
Click Next.
Click Next.
Select the first option and click Next.
Click Next.
Choose the XSD files and click Next.
Choose the “Use XSLT to define the handshake” option, click the plus green icon to create Request and Response XSLT files and click Finish.
Drag the arrow from socketService and drop within the pipeline area.
Click Next.
Click Finish.
Double-click socketServicePipeline and configure it like the image below.
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>
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>
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>
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.
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(); } } }
You may use log component to validate the message.