use of javax.naming.InitialContext in project jBPM5-Developer-Guide by Salaboy.
the class MultiSessionsPatternsTest method multiSessionsWithSessionLocator.
/**
* This test uses the concept of a SessionLocator to register slaves sessions.
* Based on rules, the master session decides which (process definition),
* when (declaratively expressed with rules) and where (in which slave session)
* to start a process instance.
*/
@Test
public void multiSessionsWithSessionLocator() throws Exception {
EntityManager em = getEmf().createEntityManager();
UserTransaction ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
StatefulKnowledgeSession interactionSession = null;
BusinessEntity interactionSessionEntity = null;
try {
// This needs to happen in the same transaction if I want to keep it consistent
ut.begin();
interactionSession = createProcessInteractionKnowledgeSession("InteractionSession", em);
interactionSessionEntity = new BusinessEntity(interactionSession.getId(), 0, 0, "InteractionSession");
// I need to join the Drools/jBPM transaction
em.joinTransaction();
em.persist(interactionSessionEntity);
ut.commit();
} catch (Exception e) {
System.out.println("Rolling Back because of: " + e.getMessage());
ut.rollback();
}
assertNotNull(interactionSessionEntity);
assertNotNull(interactionSessionEntity.getId());
interactionSession.dispose();
// Let's create a session which contains a process and register it in the interaction session:
StatefulKnowledgeSession processSession = createProcessOneKnowledgeSessionAndRegister("My Business Unit Session", interactionSessionEntity, em);
processSession.dispose();
Person person = new Person("Salaboy", 29);
Map<String, Object> params = new HashMap<String, Object>();
params.put("person", person);
interactionSession = loadKnowldgeSession(interactionSessionEntity.getSessionId(), "InteractionSession", em);
KnowledgeRuntimeLoggerFactory.newConsoleLogger(interactionSession);
interactionSession.setGlobal("em", em);
interactionSession.setGlobal("ksessionSupport", this);
// Let's insert a fact in the master session and let the rules choose the appropriate session for us to start a process
interactionSession.insert(person);
// The process will be selected and started. Because it contains an async activity a new BusinessEntity will be created
interactionSession.fireAllRules();
// Look for all the pending Business Keys which represent an interaction and insert them into the interaction session
List<BusinessEntity> pendingBusinessEntities = em.createQuery("select be from BusinessEntity be where " + " be.active = true").getResultList();
for (BusinessEntity be : pendingBusinessEntities) {
if (!be.getBusinessKey().equals("InteractionSession")) {
interactionSession.insert(be);
}
}
// As soon as we add Data the completion will be triggered and the process will continue
interactionSession.insert(new Data());
interactionSession.fireAllRules();
interactionSession.dispose();
}
use of javax.naming.InitialContext in project javaee7-samples by javaee-samples.
the class MyResource method getList.
// @Resource(name = "DefaultManagedThreadFactory")
// ManagedThreadFactory threadFactory;
@GET
public void getList(@Suspended final AsyncResponse ar) throws NamingException {
ar.setTimeoutHandler(new TimeoutHandler() {
@Override
public void handleTimeout(AsyncResponse ar) {
ar.resume("Operation timed out");
}
});
ar.setTimeout(4000, TimeUnit.MILLISECONDS);
ar.register(new MyCompletionCallback());
ar.register(new MyConnectionCallback());
ManagedThreadFactory threadFactory = (ManagedThreadFactory) new InitialContext().lookup("java:comp/DefaultManagedThreadFactory");
Executors.newSingleThreadExecutor(threadFactory).submit(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(3000);
ar.resume(response[0]);
} catch (InterruptedException ex) {
}
}
});
}
use of javax.naming.InitialContext in project hibernate-orm by hibernate.
the class JndiRegionFactoryTest method afterStandardServiceRegistryBuilt.
@Override
protected void afterStandardServiceRegistryBuilt(StandardServiceRegistry ssr) {
if (bindToJndi) {
try {
// Create an in-memory jndi
namingServer = new SingletonNamingServer();
namingMain = new Main();
namingMain.setInstallGlobalService(true);
namingMain.setPort(-1);
namingMain.start();
props = new Properties();
props.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
props.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
final String cfgFileName = (String) ssr.getService(ConfigurationService.class).getSettings().get(InfinispanRegionFactory.INFINISPAN_CONFIG_RESOURCE_PROP);
manager = new DefaultCacheManager(cfgFileName == null ? InfinispanRegionFactory.DEF_INFINISPAN_CONFIG_RESOURCE : cfgFileName, false);
Context ctx = new InitialContext(props);
bind(JNDI_NAME, manager, EmbeddedCacheManager.class, ctx);
} catch (Exception e) {
throw new RuntimeException("Failure to set up JNDI", e);
}
}
}
use of javax.naming.InitialContext in project jersey by jersey.
the class ThreadLocalNamedInvoker method invoke.
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// if no instance yet exists for the current thread then look one up and stash it
if (this.get() == null) {
Context ctx = new InitialContext();
T t = (T) ctx.lookup(name);
this.set(t);
}
return super.invoke(proxy, method, args);
}
use of javax.naming.InitialContext in project quickstarts by jboss-switchyard.
the class CamelJMSBindingTest method sendTextToQueue.
private void sendTextToQueue(final String text, final String queueName) throws Exception {
InitialContext initialContext = null;
Connection connection = null;
Session session = null;
MessageProducer producer = null;
try {
initialContext = new InitialContext();
final Queue testQueue = (Queue) initialContext.lookup(queueName);
final ConnectionFactory connectionFactory = (ConnectionFactory) initialContext.lookup("ConnectionFactory");
connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
producer = session.createProducer(testQueue);
producer.send(session.createTextMessage(text));
} finally {
if (producer != null) {
producer.close();
}
if (session != null) {
session.close();
}
if (connection != null) {
connection.close();
}
if (initialContext != null) {
initialContext.close();
}
}
}
Aggregations