Java provides ThreadGroup class. A thread group represents a set of threads. In addition, a thread group can also include other thread groups. The thread groups form a tree in which every thread group except the initial thread group has a parent.
Extend this class and implement uncaughtException(Thread t, Throwable e) method. This method would be called by the Java Virtual Machine when a thread in this thread group stops because of an uncaught exception.
Create child thread add this thread to a thread group.
Code below explains this further.
class ThreadExample extends Thread
{
String Request;
ThreadExample(ThreadGroup tg,String varRequestID)
{
super(tg,varRequestID);
Request = varRequestID;
System.out.println(this);
start();
}
public void run()
{
System.out.println("Request " + Request + " started");
try
{
Thread.sleep(5000);
}
catch(Exception e)
{
}
// let us create out of memory exception
String s = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
while(true)
{
s = s + s + s + s;
}
}
}
class Test
{
public static class ThreadGroupExample extends ThreadGroup {
public ThreadGroupExample(String s) {
super(s);
}
public void uncaughtException(Thread thread, Throwable throwable) {
System.out.println("Thread " + thread.getName()
+ " died, exception was: ");
throwable.printStackTrace();
}
}
public static void main(String args[])
{
int i = 1;
// Create thread group
ThreadGroupExample tg = new ThreadGroupExample("JC");
while (i <= 4) { try { // Create new thread and add to thread group ThreadExample objDEE = new ThreadExample(tg,Integer.toString(i)); System.out.println("ThreadGroupName: " + objDEE.getThreadGroup().getName()); } catch (Exception e) { System.out.println("Main"); e.printStackTrace(); } i = i + 1; } // list number of running thread in thread group while(true) { int tCount = tg.activeCount(); System.out.println("Thread Count " + Integer.toString(tCount)); Thread[] tArray = new Thread[tCount + 2]; int count = tg.enumerate(tArray); for(int j = 1;j <= count;j++) { System.out.println(tArray[j-1].toString()); } try { Thread.sleep(2000); } catch(Exception e) { } } } }
Saturday, July 4, 2009
Monday, March 16, 2009
Java Connector - Java ActiveX bridge example
Below is a simple example which uses ActiveX Bridge and Java Connector to connect to SAP Server using C#.
Software Pre-requisites
SAP Java Connector Version 3.0
JDK version: jdk-1_5_0_17-windows-i586-p
Microsoft Visual C++ 6.0 with SP3
Visual Studio 2003/2005/2008
Folder structure and installation assumption
JDK installed at: D:\java
JRE installed at: C:\Program Files\Java
Working folder for the program: C:\JDk2
Package structure for the java program: C:\JDk2\app\com
i.e. Our java programs would be under package app.com
Steps to create a Java Bean which uses SAP Java connector, compile and test the bean
Add “D:\Java\bin” to DOS path environment variable.
Copy sapjco3.jar file from SAP java connector unzipped folder to C:\JDK2
Copy sapjco3.dll from SAP java connector unzipped folder to C:\WINDOWS\system32
Use C:\JDK2\app\com as working folder for your java programs
Compile java program using following command
C:\JDk2>javac app\com\Person.java
Execute program using following command
C:\JDk2>java app.com.Person i.e. Person is the name of class in package app.com
Steps to create ActiveX dll using Java ActiveX Bridge
Create manifest file for jar file as below Manifest-
Version: 1.0 Created-By: 1.2 (Sun Microsystems Inc.)
Name: app/com/Person.class Java-Bean: True
Name: sapjco3.jar Java-Bean: false
Create JAR file using following command
C:\JDk2>D:\java\bin\jar -cvfm Person.jar C:\jdk2\app\com\person_manifest app\com\Person.class sapjco3.jar
Execute VCVARS32.BAT file from following installation path of Microsoft Visual C++ 6.0 C:\JDk2>C:\Program Files\Microsoft Visual Studio\VC98\Bin\ VCVARS32.BAT
Create ActiveX DLL using packager.exe utility
C:\JDk2>D:\Java\bin\packager -clsid {D824B185-AE3C-11D6-ABF5-00B0D07B8581} -out "C:\Program Files\Java\jre1.5.0_17\axbridge\bin" -reg C:\jdk2\Person.jar app.com.Person
Steps to register and call ActiveX dll in .NET C# application
Register the new DLL using following command
C:\JDk2>c:\WINDOWS\system32\regsvr32.exe /u "c:\Program Files\Java\jre1.5.0_17\a xbridge\bin\Person.dll"
Copy “sapjco3.jar” to the C:\Program Files\Java\jre1.5.0_17\lib\ext directory
Create a new Console project from Visual studio.
Click on “Add reference” under “com” tab select “person Bean Control”
Add following code to the Main method
Person.PersonDispatch p = new Person.PersonClass();
p.setFirstName("E");
p.setLastName(" bean");
string s = p.getName();
Execute the .NET C# application Below is code for Person.Java
//We need this for the Serializable.
package approva.com;import java.io.*;
import java.io.File;import java.io.FileOutputStream;
import java.util.Properties;
import java.util.concurrent.CountDownLatch;
import com.sap.conn.jco.AbapException;
import com.sap.conn.jco.JCoContext;
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.JCoField;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoFunctionTemplate;
import com.sap.conn.jco.JCoStructure;
import com.sap.conn.jco.JCoTable;
import com.sap.conn.jco.ext.DestinationDataProvider;
//We need this for the Swing.import javax.swing.*;
//To be a JavaBean our class MUST implement Serializable.
public class Person implements Serializable
{ private String fName,lName;
//Our fairly unexciting constructor.
static String ABAP_AS = "ABAP_AS_WITHOUT_POOL";
static String ABAP_AS_POOLED = "ABAP_AS_WITH_POOL";
static String ABAP_MS = "ABAP_MS_WITHOUT_POOL";
static
{
Properties connectProperties = new Properties();
connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, ""); connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, ""); connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, ""); connectProperties.setProperty(DestinationDataProvider.JCO_USER, ""); connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, ""); connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "");
createDataFile(ABAP_AS, "jcoDestination", connectProperties); }
static void createDataFile(String name, String suffix, Properties properties)
{
File cfg = new File(name+"."+suffix);
if(!cfg.exists())
{
try
{
FileOutputStream fos = new FileOutputStream(cfg, false);
properties.store(fos, "for tests only !");
fos.close();
}
catch (Exception e)
{
throw new RuntimeException("Unable to create the destination file " + cfg.getName(), e);
}
}
}
public static void step1Connect() throws JCoException
{
JCoDestination destination = JCoDestinationManager.getDestination(ABAP_AS); System.out.println("Attributes:");
System.out.println(destination.getAttributes());
System.out.println();
}
public static void step4WorkWithTable() throws JCoException
{
JCoDestination destination = JCoDestinationManager.getDestination(ABAP_AS);
JCoFunction function = destination.getRepository().getFunction("BAPI_COMPANYCODE_GETLIST");
if(function == null) throw new RuntimeException("BAPI_COMPANYCODE_GETLIST not found in SAP.");
try{
function.execute(destination);
}
catch(AbapException e)
{
System.out.println(e.toString());
return;
}
JCoStructure returnStructure = function.getExportParameterList().getStructure("RETURN"); if (! (returnStructure.getString("TYPE").equals("")returnStructure.getString("TYPE").equals("S")) )
{
throw new RuntimeException(returnStructure.getString("MESSAGE"));
}
JCoTable codes = function.getTableParameterList().getTable("COMPANYCODE_LIST"); tblcodes = function.getTableParameterList().getTable("COMPANYCODE_LIST");
for (int i = 0; i <> { codes.setRow(i); System.out.println(codes.getString("COMP_CODE") + '\t' + codes.getString("COMP_NAME"));} }
public static JCoTable tblcodes;
public Person()
{
fName = new String();
lName = new String();
}
//Here we have two basic setter methods for our two fields.
public void setFirstName(String firstName)
{
fName = firstName;
}
public void setLastName(String lastName)
{
lName = lastName;
}
//Here we have our getter methods that makes use of our two fields.
public String getName()
{
return fName+" "+lName;
}
public JCoTable getJCoTable()
{
return tblcodes;
}
public static void main(String[] args) throws JCoException
{
step1Connect();
step4WorkWithTable();
}
}
Thanks E..
Software Pre-requisites
SAP Java Connector Version 3.0
JDK version: jdk-1_5_0_17-windows-i586-p
Microsoft Visual C++ 6.0 with SP3
Visual Studio 2003/2005/2008
Folder structure and installation assumption
JDK installed at: D:\java
JRE installed at: C:\Program Files\Java
Working folder for the program: C:\JDk2
Package structure for the java program: C:\JDk2\app\com
i.e. Our java programs would be under package app.com
Steps to create a Java Bean which uses SAP Java connector, compile and test the bean
Add “D:\Java\bin” to DOS path environment variable.
Copy sapjco3.jar file from SAP java connector unzipped folder to C:\JDK2
Copy sapjco3.dll from SAP java connector unzipped folder to C:\WINDOWS\system32
Use C:\JDK2\app\com as working folder for your java programs
Compile java program using following command
C:\JDk2>javac app\com\Person.java
Execute program using following command
C:\JDk2>java app.com.Person i.e. Person is the name of class in package app.com
Steps to create ActiveX dll using Java ActiveX Bridge
Create manifest file for jar file as below Manifest-
Version: 1.0 Created-By: 1.2 (Sun Microsystems Inc.)
Name: app/com/Person.class Java-Bean: True
Name: sapjco3.jar Java-Bean: false
Create JAR file using following command
C:\JDk2>D:\java\bin\jar -cvfm Person.jar C:\jdk2\app\com\person_manifest app\com\Person.class sapjco3.jar
Execute VCVARS32.BAT file from following installation path of Microsoft Visual C++ 6.0 C:\JDk2>C:\Program Files\Microsoft Visual Studio\VC98\Bin\ VCVARS32.BAT
Create ActiveX DLL using packager.exe utility
C:\JDk2>D:\Java\bin\packager -clsid {D824B185-AE3C-11D6-ABF5-00B0D07B8581} -out "C:\Program Files\Java\jre1.5.0_17\axbridge\bin" -reg C:\jdk2\Person.jar app.com.Person
Steps to register and call ActiveX dll in .NET C# application
Register the new DLL using following command
C:\JDk2>c:\WINDOWS\system32\regsvr32.exe /u "c:\Program Files\Java\jre1.5.0_17\a xbridge\bin\Person.dll"
Copy “sapjco3.jar” to the C:\Program Files\Java\jre1.5.0_17\lib\ext directory
Create a new Console project from Visual studio.
Click on “Add reference” under “com” tab select “person Bean Control”
Add following code to the Main method
Person.PersonDispatch p = new Person.PersonClass();
p.setFirstName("E");
p.setLastName(" bean");
string s = p.getName();
Execute the .NET C# application Below is code for Person.Java
//We need this for the Serializable.
package approva.com;import java.io.*;
import java.io.File;import java.io.FileOutputStream;
import java.util.Properties;
import java.util.concurrent.CountDownLatch;
import com.sap.conn.jco.AbapException;
import com.sap.conn.jco.JCoContext;
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.JCoField;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoFunctionTemplate;
import com.sap.conn.jco.JCoStructure;
import com.sap.conn.jco.JCoTable;
import com.sap.conn.jco.ext.DestinationDataProvider;
//We need this for the Swing.import javax.swing.*;
//To be a JavaBean our class MUST implement Serializable.
public class Person implements Serializable
{ private String fName,lName;
//Our fairly unexciting constructor.
static String ABAP_AS = "ABAP_AS_WITHOUT_POOL";
static String ABAP_AS_POOLED = "ABAP_AS_WITH_POOL";
static String ABAP_MS = "ABAP_MS_WITHOUT_POOL";
static
{
Properties connectProperties = new Properties();
connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, ""); connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, ""); connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, ""); connectProperties.setProperty(DestinationDataProvider.JCO_USER, ""); connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, ""); connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "");
createDataFile(ABAP_AS, "jcoDestination", connectProperties); }
static void createDataFile(String name, String suffix, Properties properties)
{
File cfg = new File(name+"."+suffix);
if(!cfg.exists())
{
try
{
FileOutputStream fos = new FileOutputStream(cfg, false);
properties.store(fos, "for tests only !");
fos.close();
}
catch (Exception e)
{
throw new RuntimeException("Unable to create the destination file " + cfg.getName(), e);
}
}
}
public static void step1Connect() throws JCoException
{
JCoDestination destination = JCoDestinationManager.getDestination(ABAP_AS); System.out.println("Attributes:");
System.out.println(destination.getAttributes());
System.out.println();
}
public static void step4WorkWithTable() throws JCoException
{
JCoDestination destination = JCoDestinationManager.getDestination(ABAP_AS);
JCoFunction function = destination.getRepository().getFunction("BAPI_COMPANYCODE_GETLIST");
if(function == null) throw new RuntimeException("BAPI_COMPANYCODE_GETLIST not found in SAP.");
try{
function.execute(destination);
}
catch(AbapException e)
{
System.out.println(e.toString());
return;
}
JCoStructure returnStructure = function.getExportParameterList().getStructure("RETURN"); if (! (returnStructure.getString("TYPE").equals("")returnStructure.getString("TYPE").equals("S")) )
{
throw new RuntimeException(returnStructure.getString("MESSAGE"));
}
JCoTable codes = function.getTableParameterList().getTable("COMPANYCODE_LIST"); tblcodes = function.getTableParameterList().getTable("COMPANYCODE_LIST");
for (int i = 0; i <> { codes.setRow(i); System.out.println(codes.getString("COMP_CODE") + '\t' + codes.getString("COMP_NAME"));} }
public static JCoTable tblcodes;
public Person()
{
fName = new String();
lName = new String();
}
//Here we have two basic setter methods for our two fields.
public void setFirstName(String firstName)
{
fName = firstName;
}
public void setLastName(String lastName)
{
lName = lastName;
}
//Here we have our getter methods that makes use of our two fields.
public String getName()
{
return fName+" "+lName;
}
public JCoTable getJCoTable()
{
return tblcodes;
}
public static void main(String[] args) throws JCoException
{
step1Connect();
step4WorkWithTable();
}
}
Thanks E..
Subscribe to:
Posts (Atom)