Créez une tâche qui envoie un message JMS (Java™ Message
Service) à une file d'attente ou une rubrique. L'API du planificateur et l'API MBean WASScheduler prennent en charge différentes implémentations
de l'interface TaskInfo, chacune permettant de planifier un type de travail particulier.
Pourquoi et quand exécuter cette tâche
Pour créer une tâche qui envoie un message JMS (Java Message Service) à une file d'attente ou une rubrique, procédez comme suit :
Procédure
- Créez une instance de l'interface MessageTaskInfo en utilisant la méthode de fabrique Scheduler.createTaskInfo(). A l'aide d'un fichier JSP (JavaServer Pages), d'un servlet ou d'un conteneur EJB, créez l'instance comme indiqué dans l'exemple de code suivant :
//lookup the scheduler to be used
Scheduler scheduler = (Scheduler)new InitialContext.lookup("java:comp/env/Scheduler");
MessageTaskInfo taskInfo = (MessageTaskInfo) scheduler.createTaskInfo(MessageTaskInfo.class);
You can also use the wsadmin tool, create the instance as shown in the following JACL
scripting example:# Sample create a task using MessageTaskInfo task type
# Call this mbean with the following parameters:
# <scheduler jndiName> = JNDI name of the scheduler resource,
# for example scheduler/myScheduler
# <JNDI name of the QCF> = The global JNDI name of the Queue Connection Factory.
# <JNDI name of the Queue> = The global JNDI name of the Queue destination
set jndiName [lindex $argv 0]
set jndiName_QCF [lindex $argv 1]
set jndiName_Q [lindex $argv 2]
# Map the JNDI name to the mbean name. The mbean name is formed by replacing the / in the jndi name
# with . and prepending Scheduler_
regsub -all {/} $jndiName "." jndiName
set mbeanName Scheduler_$jndiName
puts "Looking-up Scheduler MBean $mbeanName"
set sched [$AdminControl queryNames WebSphere:*,type=WASScheduler,name=$mbeanName]
puts $sched
# Get the ObjectName format of the Scheduler MBean
set schedO [$AdminControl makeObjectName $sched]
# Create a MessageTaskInfo object using invoke_jmx
puts "Creating MessageTaskInfo"
set params [java::new {java.lang.Object[]} 1]
$params set 0 [java::field com.ibm.websphere.scheduler.MessageTaskInfo class]
set sigs [java::new {java.lang.String[]} 1]
$sigs set 0 java.lang.Class
set ti [$AdminControl invoke_jmx $schedO createTaskInfo $params $sigs]
set mti [java::cast com.ibm.websphere.scheduler.MessageTaskInfo $ti]
puts "Created the MessageTaskInfo object: $mti"
Avertissement : La création d'un objet MessageTaskInfo n'ajoute pas la tâche dans le magasin persistant. Elle crée plutôt une marque de réservation pour les données nécessaires. La tâche n'est ajoutée dans le magasin persistant que lorsque la méthode create() est appelée sur un planificateur, comme décrit dans la rubrique Soumission d'une tâche à un planificateur.
- Définissez des paramètres au niveau de l'objet MessageTaskInfo. L'interface TaskInfo contient plusieurs méthodes set() qui permettent de contrôler l'exécution de la tâche, y compris l'heure d'exécution de la tâche et le travail qu'elle effectue à son démarrage.
L'interface TaskInfo indique d'autres paramètres de comportement,
décrits dans la documentation sur les API. A l'aide d'un fichier JSP (JavaServer Pages), d'un servlet ou d'un conteneur EJB, créez l'instance comme indiqué dans l'exemple de code suivant :
//create a date object which represents 30 seconds from now
java.util.Date startDate = new java.util.Date(System.currentTimeMillis()+30000);
//now set the start time and the JNDI names for the queue connection factory and the queue
taskInfo.setConnectionFactoryJndiName("jms/MyQueueConnectionFactory");
taskInfo.setDestination("jms/MyQueue");
taskInfo.setStartTime(startDate);
Vous pouvez également utiliser l'outil wsadmin
pour créer l'instance comme indiqué dans l'exemple de script JACL suivant :# Setup the task
puts "Setting up the task..."
# Set the startTime if you want the task to run at a specific time, for example:
$mti setStartTime [java::new {java.util.Date long} [java::call System currentTimeMillis]]
# Set the StartTimeInterval so the task runs in 30 seconds from now
$mti setStartTimeInterval 30seconds
# Set the global JNDI name of the QCF & Queue to send the message to.
$mti setConnectionFactoryJndiName $jndiName_QCF
$mti setDestinationJndiName $jndiName_Q
# Set the message
$mti setMessageData "Test Message"
# Do not purge the task when it's complete
$mti setAutoPurge false
# Set the name of the task. This can be any string value.
$mti setName Created_by_MBean
# If the task needs to run with specific authorization you can set the tasks Authentication Alias
# Authentication aliases are created using the Admin Console.
# $mti setAuthenticationAlias {myRealm/myAlias}
puts "Task setup completed."
Résultats
Un objet MessageTaskInfo a été créé. Il contient toutes les données appropriées pour qu'une tâche envoie un message JMS.
Que faire ensuite
Soumettez la tâche à un planificateur à des fins de création, comme décrit dans la rubrique Soumission d'une tâche à un planificateur.