CreateTaskEvent (TdbmEvent)

De Wiki1000
(Différences entre les versions)
Ligne 27 : Ligne 27 :
  
 
<source lang='delphi'>
 
<source lang='delphi'>
 +
 +
// call back class to be executed on event
 +
Type
 +
  OnMyOperationEvent = class(TitTask)
 +
    function Execute():Integer;
 +
    function RegisterThisTask;
 +
  end;
 +
 +
function OnMyOperationEvent.Execute():Integer;
 +
var idx:Integer; S:string; aMsg:TdbmQueueMessage;
 
begin
 
begin
 +
  Result := True;
 +
 +
  // Add messages to the log
 +
  ProgressMessage('On Task event');
 +
 +
  if Assigned(UserContext.TaskContext) and Assigned(userContext.TaskContext.EventContext) then
 +
  begin
 +
    userContext.TaskContext.AddMessage('Task context');
 +
    //
 +
    aMsg := userContext.TaskContext.EventContext.ReceivedMsg;
 +
    if Assigned(aMsg) then
 +
      begin
 +
        // get message parameters
 +
        //
 +
        userContext.TaskContext.AddMessage('Task context has message');
 +
        userContext.TaskContext.AddMessage(Format('Message parameter count:%d',[aMsg.Count]));
 +
        for idx:=0 to aMsg.Count-1 do
 +
        begin
 +
          S := aMsg.DisplayStrings(idx);
 +
          UserContext.TaskContext.AddMessage(S);
 +
        end;
 +
      end;
 +
  end
 +
  else ProgressMessage('no task context');
 +
 +
  Result := True;
 +
end;
 +
 +
function OnMyOperation.RegisterThisTask;
 +
var aReason:string;
 +
begin
 +
  if not SubscribeToEvent('task.myoperation.start',aReason) then
 +
  showMessage('subscribe failed '+aReason);
 +
end;
 +
 +
// operation code which fire event
 +
//
 +
procedure MyOperation.Start;
 +
begin
 +
  doStart(...);
 +
 +
  // create start event
 
   // => id = $myqueue.task.myoperation.start
 
   // => id = $myqueue.task.myoperation.start
   TdbmEvent.CreateTaskEvent('myqueue', 'myoperation.start', ['name1','name2'],['value1',2]);
+
  //
 +
   TdbmEvent.CreateTaskEvent('myqueue_not_used_by_task', 'myoperation.start', ['name1','name2'],['value1',2]);
 
end;
 
end;
 
</source>
 
</source>

Version du 4 juin 2019 à 15:25

class procedure CreateTaskEvent(const iQueuName:string; const iParameter:string; iNames:Array of variant; iValues:Array of variant);

Cette méthode de classe crée un évènement de type tâche.

Le type de l'évènement est TASK

iQueueName Nom de la file d'attente
iParameter Paramètre de l'identifiant du message
iNames Tableau de noms de paramètre de l'évènement
iValues Tableau de valeurs de paramètre de l'évènement

L'identifiant de l'évènement sera :

$iQueueName.task.iParameter

Exemple :

// call back class to be executed on event
Type
  OnMyOperationEvent = class(TitTask)
    function Execute():Integer;
    function RegisterThisTask;
  end;
 
function OnMyOperationEvent.Execute():Integer;
var idx:Integer; S:string; aMsg:TdbmQueueMessage;
begin
  Result := True;
 
  // Add messages to the log
  ProgressMessage('On Task event');
 
  if Assigned(UserContext.TaskContext) and Assigned(userContext.TaskContext.EventContext) then
   begin
     userContext.TaskContext.AddMessage('Task context');
     //
     aMsg := userContext.TaskContext.EventContext.ReceivedMsg;
     if Assigned(aMsg) then
      begin
        // get message parameters
        //
        userContext.TaskContext.AddMessage('Task context has message');
        userContext.TaskContext.AddMessage(Format('Message parameter count:%d',[aMsg.Count]));
        for idx:=0 to aMsg.Count-1 do
         begin
           S := aMsg.DisplayStrings(idx);
           UserContext.TaskContext.AddMessage(S);
         end;
      end;
   end
   else ProgressMessage('no task context');
 
  Result := True;
end;
 
function OnMyOperation.RegisterThisTask;
var aReason:string;
begin
  if not SubscribeToEvent('task.myoperation.start',aReason) then
   showMessage('subscribe failed '+aReason); 
end;
 
// operation code which fire event
//
procedure MyOperation.Start;
begin
  doStart(...);
 
  // create start event 
  // => id = $myqueue.task.myoperation.start
  //
  TdbmEvent.CreateTaskEvent('myqueue_not_used_by_task', 'myoperation.start', ['name1','name2'],['value1',2]);
end;

Voir aussi :

Outils personnels