package concurrency;public class DeadLock {public static void main(String[] args) {JavaFile firstFile = new JavaFile("file1");JavaFile secondFile = new JavaFile("file2");JavaCoder firstCode = new JavaCoder(firstFile, secondFile, "first coder:");JavaCoder secondCoder = new JavaCoder(secondFile, firstFile, "second coder:");firstCode.start();secondCoder.start();}}class JavaCoder extends Thread {public void run() {System.out.println(Thread.currentThread().getName() + "trying to checkin " + firstfile);this.firstfile.checkIn(this.secondfile);}private JavaFile secondfile;private JavaFile firstfile;JavaCoder(JavaFile firstFile, JavaFile secondFile, String name) {super(name);this.firstfile = firstFile;this.secondfile = secondFile;}};class JavaFile {private String name;JavaFile(String name) {this.name = name;}@Overridepublic String toString() {return this.name;}public synchronized void checkIn(JavaFile otherFile) {System.out.println(Thread.currentThread().getName() + "in check-in " + this);otherFile.checkOut();System.out.println(Thread.currentThread().getName() + "completing check-in " + this);}public synchronized void checkOut() {System.out.println(Thread.currentThread().getName() + "in check-out " + this);while (true) {}}};Sample Outputfirst coder:trying to checkin file1first coder:in check-in file1first coder:in check-out file2second coder:trying to checkin file2 ( This is the dead lock , second coder is trying to checkin file 2 which is locked by first coder for check-out)
Labels
Sample program in java for Deadlock
Stop a thread in java
static boolean stopRequested = false;public static synchronized boolean isStopped() {return stopRequested;}public static synchronized void stopRequested() {StopThread.stopRequested = true;}public static void main(String ar[]) throws InterruptedException {Thread runningThread = new Thread(new Runnable() {@Overridepublic void run() {while (!isStopped()) {System.out.println(stopRequested);}}});runningThread.start();System.out.println("Stop Running Thread");stopRequested();}
Effective JUNIT's
PIT
http://pitest.org/ best place to start
Mutations
supported and The details of each type of mutation are here
not
all are enabled by default (please check I am not sure)
How
to use with Maven
1) Add the plugin
x.x.somepackage.*
2) Execute the command mvn org.pitest:pitest-maven:mutationCoverage
3) Check the report in \target\pit-reports
Sonar:
Sonar
has a plugin for pit tests which adds
these survived mutations as violations
Eclipse :
To
install pit for eclipse refer
Java Object Class
it is the base class of all the classes in java
Methods in Java Object class
- hashCode:
- the more unique the hashCode is the more efficient is the hash map/table
- used to find the bucket in which an object goes into in a hash table.
- more than one invocation of hashCode of the same object should return same Integer.
- if two objects are equal according to equals method then their hashCode must be same.
- equals:
- is used in hashMap when two objects have same hashCode.
- if two objects are equal their hashCode must be same.
- it should be
- reflexive : x.equals(x)
- symmetric: x.equals(y) and y.equals(x)
- transtive: x.equals(y) y.equals(z) implies => x.equals(z)
- consitent: x.equals(y) should allways return same value.
- for null it should be false: x.equals(null) false
- the default behaviour of equals = “==” i.e is object comparision
- toString :
- the default implementation is
- classname @ hex format of the hashCode.
- its good to override toString .
- also should be concise and informative.
- clone:
- to clone the object .
- x.clone() != x
- x.clone().getClass() == x.getClass() Not mandatory *
- x.clone().equals(x) Not mandatory *
- alternative to clone is copy constructor
Must have Eclipse Plugins
- m2eclipse: maven plugin for eclipse
- PIT Mutation Test: this plugin helps run mutation tests in eclipse
- Sonar plugin for eclipse: run sonar analysis locally or remotely.
- Jupiter Plugin:Code Review
- TDD Plugins
- http://www.happyprog.com/pairhero/
- http://www.happyprog.com/pulse/
- http://www.happyprog.com/tdgotchi/
- infinitest: runs junit infinetlyhttp://infinitest.github.com/user_guide.html : run related junits for every change
- Static Code Analsis: PMD Programmer Mistakes detector and FindBugs for eclipse
- JUNIT Coverage : EclEmma/clover
- JUNIT junit helper, quick unit
Creating jdbc data source on weblogic using java code
1:
2: import java.net.UnknownHostException;
3:
4: import org.python.util.InteractiveInterpreter;
5:
6: import weblogic.management.scripting.utils.WLSTInterpreter;
7:
8: public class DataSourceCreator {
9:
10: static InteractiveInterpreter interpreter = null;
11: private String weblogicPassword;
12: private String weblogicUserName;
13: private StringBuffer wlst_script;
14:
15: public DataSourceCreator(String weblogicUserName, String weblogicPassword) {
16: interpreter = new WLSTInterpreter();
17: this.weblogicUserName = weblogicUserName;
18: this.weblogicPassword = weblogicPassword;
19:
20: }
21:
22: /**
23: * connect to weblogic
24: */
25: private void connect() {
26: wlst_script.append("connect('" + this.weblogicUserName + "','" + this.weblogicPassword
27: + "')");
28: }
29:
30: /**
31: * creates the data source script
32: *
33: * @param dbpassword
34: * @param dbuser
35: * @param dsname
36: * @param jndiname
37: * @param dburi
38: * @param dbdrivername
39: */
40: private void createDataSourceScript(String dbpassword, String dbuser, String dsname,
41: String jndiname, String dburi, String dbdrivername) {
42:
43: int timeout = 100000;
44: int maxcapacity = 100;
45:
46: // substitute your server name here "SERVER_NAME" i.e the target.
47: wlst_script.append("cd('Servers/SERVER_NAME')\n");
48: wlst_script.append("target=cmo\n");
49: wlst_script.append("cd('../..')\n");
50: wlst_script.append("jdbcSystemResource = create('" + dsname + "','JDBCSystemResource')\n");
51: wlst_script.append("theJDBCResource = jdbcSystemResource.getJDBCResource()\n");
52: wlst_script.append("theJDBCResource.setName('" + dsname + "')\n");
53: wlst_script
54: .append("connectionPoolParams = theJDBCResource.getJDBCConnectionPoolParams()\n");
55: wlst_script.append("connectionPoolParams.setConnectionReserveTimeoutSeconds(int('"
56: + timeout + "'))\n");
57:
58: // maximum connections that can be made
59: wlst_script.append("connectionPoolParams.setMaxCapacity(int('" + maxcapacity + "'))\n");
60: // query to execute using ds to test if its successfull
61: wlst_script.append("connectionPoolParams.setTestTableName('SQL SELECT 1 FROM DUAL')\n");
62:
63: // update data source params
64: wlst_script.append("dsParams = theJDBCResource.getJDBCDataSourceParams()\n");
65: wlst_script.append("dsParams.addJNDIName('" + jndiname + "')\n");
66:
67: wlst_script.append("driverParams = theJDBCResource.getJDBCDriverParams()\n");
68: wlst_script.append("driverParams.setUrl('" + dburi + "')\n");
69: wlst_script.append("driverParams.setDriverName('" + dbdrivername + "')\n");
70: wlst_script.append("driverParams.setPassword('" + dbpassword + "')\n");
71:
72: wlst_script.append("driverProperties = driverParams.getProperties()\n");
73: wlst_script.append("proper = driverProperties.createProperty('user')\n");
74: wlst_script.append("proper.setValue('" + dbuser + "')\n");
75: wlst_script.append("proper1 = driverProperties.createProperty('DatabaseName')\n");
76: wlst_script.append("proper1.setValue('" + dburi + "')\n");
77: wlst_script.append("jdbcSystemResource.addTarget(target)\n");
78: }
79:
80: /**
81: * @param wlst_script
82: */
83: private void saveActivateAndDisconnect() {
84: // save , activate changes and disconnect
85: wlst_script.append("save()\n");
86: wlst_script.append("activate(block='true')\n");
87: wlst_script.append("disconnect()\n");
88: }
89:
90: /**
91: * start edit
92: *
93: * @param wlst_script
94: */
95: private void startEdit() {
96: wlst_script.append("edit()\n");
97: wlst_script.append("startEdit()\n");
98: }
99:
100: public static void main(String args[]) throws UnknownHostException {
101:
102: // weblogic related
103: String weblogicUserName = "username";
104: String weblogicPassword = "password";
105:
106: DataSourceCreator dsCreator = new DataSourceCreator(weblogicUserName, weblogicPassword);
107:
108: dsCreator.connect();
109: dsCreator.startEdit();
110: String sid = "sidofdb"; // sid of db
111: String ipaddress = "10.8.11.16"; // ip addres
112: String dbpassword = "password"; // database password
113: String dbuser = "username"; // db user name
114: String dsname = "datasourcename"; // your data source name
115: String jndiname = "jdbc/" + dsname; // jndi name for ds
116: String dburi = "jdbc:oracle:thin:@" + ipaddress + ":1521:" + sid;
117: String dbdrivername = "oracle.jdbc.driver.OracleDriver";
118:
119: dsCreator.createDataSourceScript(dbpassword, dbuser, dsname, jndiname, dburi, dbdrivername);
120: dsCreator.saveActivateAndDisconnect();
121:
122: try {
123: // execute the script created above
124: interpreter.exec(dsCreator.wlst_script.toString());
125: } finally {
126: // disconnect even if there is exception
127: interpreter.exec("disconnect()\n");
128: }
129:
130: }
131:
132: }
\Java\jre6\lib\ext\QTJava.zip was unexpected at this time.
just check if "Java\jre6\lib\ext\QTJava.zip" is there in the class path you just have to remove and restart weblogic.
Roman Number to decimal
Map mapping = new HashMap();
Conversion() {
mapping.put("I", 1);
mapping.put("V", 5);
mapping.put("X", 10);
mapping.put("L", 50);
mapping.put("C", 100);
mapping.put("D", 500);
mapping.put("M", 1000);
}
/**
* @param romanNumber
* @return
*/
public int romanToDecimal(String romanNumber) {
int result = 0;
if (romanNumber.length() == 0) {
return result;
}
if (romanNumber.length() == 1) {
return (Integer) mapping.get(romanNumber);
}
else {
int leftEnd = (Integer) mapping.get(romanNumber.charAt(0) + "");
if (leftEnd >= (Integer) mapping.get(romanNumber.charAt(1) + "")) {
result += leftEnd;
} else {
result += - leftEnd;
}
result += romanToDecimal(romanNumber.substring(1));
}
return result;
}
Conversion() {
mapping.put("I", 1);
mapping.put("V", 5);
mapping.put("X", 10);
mapping.put("L", 50);
mapping.put("C", 100);
mapping.put("D", 500);
mapping.put("M", 1000);
}
/**
* @param romanNumber
* @return
*/
public int romanToDecimal(String romanNumber) {
int result = 0;
if (romanNumber.length() == 0) {
return result;
}
if (romanNumber.length() == 1) {
return (Integer) mapping.get(romanNumber);
}
else {
int leftEnd = (Integer) mapping.get(romanNumber.charAt(0) + "");
if (leftEnd >= (Integer) mapping.get(romanNumber.charAt(1) + "")) {
result += leftEnd;
} else {
result += - leftEnd;
}
result += romanToDecimal(romanNumber.substring(1));
}
return result;
}
Invoke a webservice from linux shell
ENDPOINT="http://localhost:8001/soa-infra/services/default/App/StartApp"
curl --silent --data @- --header 'Content-Type: application/soap+xml; charset=utf-8' --user-agent "" ${ENDPOINT} <<EOF
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://xmlns.oracle.com/Application/StartApp">
<soap12:Body>
<ns1:process>
<ns1:input>running</ns1:input>
</ns1:process>
</soap12:Body>
</soap12:Envelope>
EOF
ENDPOINT : this can be configured to point to ws endpoint
The envelope in weblogic can be got by clicking test on the web service and switching to xml view.
Curl is a command line tool to transfer data to a URL
Subscribe to:
Posts (Atom)