job in parallel并发执行

[复制链接]
查看11 | 回复9 | 2005-10-30 17:05:33 | 显示全部楼层 |阅读模式
我如果有16个procedures 的话,
开始的时候,提交4个jobs并发执行:job1,job2,job3,job4
假设job2首先完成了,我想从剩余的12个procedures中选取一个在做成一个job运行,不知道各位有没有经历过呢。
我测试了“dbms_alert”这个包好久,没有成功,跑来这儿讨教来了
回复

使用道具 举报

千问 | 2005-10-30 17:05:33 | 显示全部楼层
up and up, 非常着急啊
回复

使用道具 举报

千问 | 2005-10-30 17:05:33 | 显示全部楼层
兄弟很着急,希望各位伸出你热情的双手来拥抱队列吧。。。。。
回复

使用道具 举报

千问 | 2005-10-30 17:05:33 | 显示全部楼层
up,up,明天要交给leader的
回复

使用道具 举报

千问 | 2005-10-30 17:05:33 | 显示全部楼层
难道这儿也没有人知道吗,
回复

使用道具 举报

千问 | 2005-10-30 17:05:33 | 显示全部楼层
最后,偶自己测试成功,供大家参考吧:
[php]
create or replace package JobsPkg as
/* Types */
subtype tString is varchar2(30);
subtype tJob is varchar2(64);
subtype tDBString is varchar2(255);
type tArrayJob is table of tJob index by binary_integer;
type tArraySeqByJob is table of number index by binary_integer;
/* Execute Executes single job by submiting it to the job queue.*/
procedure Execute(pJob in tJob, pCommit in boolean := true);
/* Execute Executes array of jobs by submiting it to the job queue.*/
procedure Execute(pJobs in tArrayJob, pCommit in boolean := true);
/* SignalCompletion Send an alert to signal job completion. */
procedure SignalCompletion(pJob in tJob, pMessage in tDBString := null);
/* WaitForCompletion---Waits for completion of all parallel jobs
* from the array pJobs passed as a parameter. */
procedure WaitForCompletion(pJobs tArrayJob,pjobstotal tArrayjob);
function BuildAlertName(pJob in tJob) return tString;
end JobsPkg;
create or replace package body JobsPkg as
/* BuildAlertName */
function BuildAlertName(pJob in tJob) return tString is
vAlert tString;
begin
if length(pJob)0 then
vAlert := upper(substr(pJob, instr(pJob, '.') + 1));
else
vAlert := upper(substr(pJob, 1, 30));
end if;
end if;
return vAlert;
end BuildAlertName;
/**Execute Executes single job by submiting it to the job queue. */
procedure Execute(pJob in tJob, pCommit in boolean := true) is
vJobtJob := ltrim(rtrim(pJob));
vJobNum binary_integer;
begin
if substr(vJob, -1) != ';' then
vJob := vJob || ';';
end if;
dbms_job.submit(vJobNum, vJob,sysdate+1/(24*60));--,'trunc(sysdate,''mi'')+ 1/(24*60)');
--DebugPkg.Log(to_char(vJobNum) || ': ' || vJob);
if pCommit then
commit;
end if;
end Execute;
/* Execute Executes set of jobs by submiting it to the job queue.*/
procedure Execute(pJobs in tArrayJob, pCommit in boolean := true) is
begin
if pJobs.count > 0 then
for nIndex in pJobs.first .. pJobs.last loop
Execute(pJobs(nIndex), (not pCommit));
end loop;

if pCommit then
commit;
end if;
end if;
end Execute;
/* SignalCompletion Send an alert to signal job completion.*/
procedure SignalCompletion(pJob in tJob, pMessage in tDBString := null) is
begin
dbms_alert.signal(BuildAlertName(pJob),

nvl(pMessage, pJob || ' completed.'));
commit;
end SignalCompletion;
/* WaitForCompletion
* Waits for completion of all parallel jobs from the array pJobs passed as a parameter.
*/
procedure WaitForCompletion(pJobs tArrayJob,pJobsTotal tArrayJob) is
vAlertstarrayjob;
vAlert tJob;
vNametJob;
vMessage tDBString;
vStatuspls_integer;
vTimeOut pls_integer := 3600;
inumber;
v_job_diff binary_integer;
begin
/* this values record the number of left unsubmit procedures */
v_job_diff:=pJobsTotal.count-pjobs.count;

if pJobsTotal.count > 0 then
/* Step 1. Build array of alerts.*/
for nIndex in pJobsTotal.first .. pJobsTotal.last loop
vAlerts(nIndex) := BuildAlertName(pJobsTotal(nindex));
end loop;

/* Step 2.Register for alerts. */
vAlert := vAlerts.first;
vname:=valerts(valert);
while (vAlert is not null) loop
vname:=valerts(valert);
dbms_alert.register(vname);
vAlert := vAlerts.next(vAlert);
end loop;

/* Step 3. Waiting until all of the alerts are received. */
i:=0;
while (vAlerts.count > 0) loop
dbms_alert.waitany(vName, vMessage, vStatus, vTimeOut);
if vStatus = 0 then

dbms_alert.remove(vName);

for vindex in valerts.first..valerts.last loop

if upper(trim(valerts(vindex)))=vName then

vAlerts.delete(vindex);

exit;

end if;

end loop;

dbms_output.put_line('Received alert: ' || vName ||

' with message: ' || vMessage);
i:=i+1;


/* heresubmit the left procedure as job */
if i<=v_job_diff then

Execute(pJobstotal(pjobs.count+i), true);


end if;

/*endsubmit the left procedure as job */

end if;
end loop;
end if;
end WaitForCompletion;
end JobsPkg;

[/php]
回复

使用道具 举报

千问 | 2005-10-30 17:05:33 | 显示全部楼层
这是测试用的包:
环境:8.1.7+windows2000
[php]
create or replace package DemoPkg is
procedure Sleep(p_char number);
end DemoPkg;
create or replace package body DemoPkg is
procedure Sleep(p_char number) is
p_char1 varchar2(50);
begin
select dbms_random.string('c', p_char) into p_char1 from dual;
--dbms_lock.sleep(pTime);
--JobsPkg.SignalCompletion('DemoPkg.Sleep(' || to_char(pTime) || ')');
JobsPkg.SignalCompletion('DemoPkg.Sleep(' || p_char || ')', p_char1);
dbms_output.put_line('DemoPkg.Sleep(' || p_char || ')' ||'======' ||p_char1);
end Sleep;
end DemoPkg;

[/php]
回复

使用道具 举报

千问 | 2005-10-30 17:05:33 | 显示全部楼层
能不能帮我看看如果下面是三个过程,JOB都提交后,怎样编失败和系统资源耗损很大时自动SLEEP好,

public void execjob1(){
String execjob1="create or replace procedure execjob1 as \n"+

"job1 number; \n"+

"begin \n"+

"dbms_job.submit(job1,'proc_1;',sysdate,'sysdate+8/(24*60)'); \n"+

"commit; \n"+

"end execjob1; \n";
try{
stmt.executeQuery(execjob1);
}catch(SQLException s){ }
}

public void execjob2(){
String execjob2="create or replace procedure execjob2 as \n"+

"job2 number; \n"+

"begin \n"+

"dbms_job.submit(job2,'proc_2;',sysdate,'sysdate+10/(24*60)'); \n"+

"commit; \n"+

"end execjob2; \n";
try{
stmt.executeQuery(execjob2);
}catch(SQLException s){ }
}

public void execjob3(){
String execjob3="create or replace procedure execjob3 as \n"+

"job3 number; \n"+

"begin \n"+

"dbms_job.submit(job3,'proc_3;',sysdate,'sysdate+12/(24*60)'); \n"+

"commit; \n"+

"end execjob3; \n";
try{
stmt.executeQuery(execjob3);
}catch(SQLException s){ }
}
回复

使用道具 举报

千问 | 2005-10-30 17:05:33 | 显示全部楼层
sorry,不是很明白,试着用pl/sql语言表示来看看。
回复

使用道具 举报

千问 | 2005-10-30 17:05:33 | 显示全部楼层
在SQL里 就是三个JOB
variable job1 number
begin
dbms_job.submit(:number,'proc_1',sysdate,'sysdate+5/(24*60)')
commit;
end;
JOB2,JOB3同上,
如果失败或负载过高时停止JOB怎么编写好,
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行