1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
public int connect(String encoder) {
connectionCommand = null;
try {
Context context = new InitialContext();
ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup("ConnectionFactory");
Connection connection = connectionFactory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = (Destination) context.lookup("queue/StockValue");
MessageProducer producer = session.createProducer(destination);
ObjectMessage message = session.createObjectMessage();
message.setObject(new ConnectionCommand(encoder));
producer.send(message);
producer.close();
session.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.toString());
return 666;
}
int retry = 20;
while(getConnectionCommand() == null && retry > 0) {
try {
Thread.sleep(200);
} catch(InterruptedException e) {
e.printStackTrace();
return 666;
}
retry--;
}
if(getConnectionCommand() == null) {
return -1;
} else {
return 0;
}
} |
Partager