Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ThreadUser extends Thread {
- // Délai de pause du Thread
- public static final int delay = 10000;
- // Context du bundle lié au Thread
- private BundleContext context = null;
- /**
- * Constructeur, le Thread a besoin du BundleContext pour rechercher le
- * service
- *
- * @param aContext
- */
- public ThreadUser(BundleContext aContext) {
- context = aContext;
- }
- /**
- * Méthode run qui sera appelée par the Thread.start()
- */
- public void run() {
- System.out.println("Demarrage du Thread User");
- int carId = 1;
- while (true) {
- repairCar(carId++);
- try {
- sleep(delay);
- } catch (InterruptedException e) {
- }
- }
- }
- /**
- * Implémentation des Traitements du Thread
- */
- private void repairCar(int carId) {
- // Création d'une voiture
- Car aCar = null;
- if (Math.random() > 0.5) {
- aCar = new Megane(carId);
- } else {
- aCar = new Lada(carId);
- }
- // Récupération du service
- ServiceReference<CarRepairer> carRepairerSR = context
- .getServiceReference(CarRepairer.class);
- if (carRepairerSR == null) {
- System.out
- .println("\n**************\nAucun service disponible");
- } else {
- System.out.println("\n**************\nAppel du service:"
- + carRepairerSR.getClass() + "\nDu bundle:"
- + carRepairerSR.getBundle().getSymbolicName()
- + "\nPour la voiture:" + aCar.getId() + ","
- + aCar.getClass() + "," + aCar.getCurrentStatus());
- CarRepairer myService = context.getService(carRepairerSR);
- // Appel du service
- try {
- myService.repairCar(aCar);
- System.out.println("La voiture a bien été traitée : "
- + aCar.getClass() + ":" + aCar.getCurrentStatus());
- } catch (CarRepairException e) {
- System.out
- .println("Erreur la voiture n'est pas au bon statut pour ce service : "
- + aCar.getClass()
- + ":"
- + aCar.getCurrentStatus());
- }
- // Libération du service
- context.ungetService(carRepairerSR);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement