我需要编写一个java代码来使用API在Weblogic中创建数据源。我从Weblogic找到了一个API。但我无法找到任何示例实现。有人可以帮我吗?我正在尝试使用以下API:
DataSourceManager dsManager = DataSourceManager.getInstance();
dsManager.createAndStartDataSource(jdbcImpl, appName, moduleName, compName, jdbcCtx, classLoader)
提前致谢。
答案 0 :(得分:0)
您需要对该操作使用WLST脚本接口管理,如该片段中所述:
3: import java.net.UnknownHostException;
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: }