Advertisement
Guest User

Untitled

a guest
May 20th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.04 KB | None | 0 0
  1. public class ThreadUser extends Thread {
  2.         // Délai de pause du Thread
  3.         public static final int delay = 10000;
  4.         // Context du bundle lié au Thread
  5.         private BundleContext context = null;
  6.  
  7.         /**
  8.          * Constructeur, le Thread a besoin du BundleContext pour rechercher le
  9.          * service
  10.          *
  11.          * @param aContext
  12.          */
  13.         public ThreadUser(BundleContext aContext) {
  14.             context = aContext;
  15.         }
  16.  
  17.         /**
  18.          * Méthode run qui sera appelée par the Thread.start()
  19.          */
  20.         public void run() {
  21.             System.out.println("Demarrage du Thread User");
  22.             int carId = 1;
  23.             while (true) {
  24.                 repairCar(carId++);
  25.                 try {
  26.                     sleep(delay);
  27.                 } catch (InterruptedException e) {
  28.                 }
  29.             }
  30.         }
  31.  
  32.         /**
  33.          * Implémentation des Traitements du Thread
  34.          */
  35.         private void repairCar(int carId) {
  36.             // Création d'une voiture
  37.             Car aCar = null;
  38.             if (Math.random() > 0.5) {
  39.                 aCar = new Megane(carId);
  40.             } else {
  41.                 aCar = new Lada(carId);
  42.             }
  43.             // Récupération du service
  44.             ServiceReference<CarRepairer> carRepairerSR = context
  45.                     .getServiceReference(CarRepairer.class);
  46.  
  47.             if (carRepairerSR == null) {
  48.                 System.out
  49.                         .println("\n**************\nAucun service disponible");
  50.             } else {
  51.  
  52.                 System.out.println("\n**************\nAppel du service:"
  53.                         + carRepairerSR.getClass() + "\nDu bundle:"
  54.                         + carRepairerSR.getBundle().getSymbolicName()
  55.                         + "\nPour la voiture:" + aCar.getId() + ","
  56.                         + aCar.getClass() + "," + aCar.getCurrentStatus());
  57.                 CarRepairer myService = context.getService(carRepairerSR);
  58.                 // Appel du service
  59.                 try {
  60.                     myService.repairCar(aCar);
  61.                     System.out.println("La voiture a bien été traitée : "
  62.  
  63.                     + aCar.getClass() + ":" + aCar.getCurrentStatus());
  64.  
  65.                 } catch (CarRepairException e) {
  66.                     System.out
  67.  
  68.                             .println("Erreur la voiture n'est pas au bon statut pour ce service : "
  69.  
  70.                                     + aCar.getClass()
  71.                                     + ":"
  72.                                     + aCar.getCurrentStatus());
  73.  
  74.                 }
  75.                 // Libération du service
  76.                 context.ungetService(carRepairerSR);
  77.             }
  78.         }
  79.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement