Search in sources :

Example 36 with InitialContext

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();
}
Also used : UserTransaction(javax.transaction.UserTransaction) HashMap(java.util.HashMap) StatefulKnowledgeSession(org.drools.runtime.StatefulKnowledgeSession) Data(com.salaboy.model.Data) BusinessEntity(com.salaboy.sessions.patterns.BusinessEntity) InitialContext(javax.naming.InitialContext) EntityManager(javax.persistence.EntityManager) Person(com.salaboy.model.Person) Test(org.junit.Test)

Example 37 with InitialContext

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) {
            }
        }
    });
}
Also used : TimeoutHandler(javax.ws.rs.container.TimeoutHandler) AsyncResponse(javax.ws.rs.container.AsyncResponse) InitialContext(javax.naming.InitialContext) ManagedThreadFactory(javax.enterprise.concurrent.ManagedThreadFactory) GET(javax.ws.rs.GET)

Example 38 with InitialContext

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);
        }
    }
}
Also used : Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) DefaultCacheManager(org.infinispan.manager.DefaultCacheManager) SingletonNamingServer(org.jnp.server.SingletonNamingServer) ConfigurationService(org.hibernate.engine.config.spi.ConfigurationService) Properties(java.util.Properties) Main(org.jnp.server.Main) InitialContext(javax.naming.InitialContext) NameNotFoundException(javax.naming.NameNotFoundException)

Example 39 with InitialContext

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);
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) InitialContext(javax.naming.InitialContext)

Example 40 with InitialContext

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();
        }
    }
}
Also used : ConnectionFactory(javax.jms.ConnectionFactory) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Queue(javax.jms.Queue) InitialContext(javax.naming.InitialContext) Session(javax.jms.Session)

Aggregations

InitialContext (javax.naming.InitialContext)1068 Test (org.junit.Test)325 EJBException (javax.ejb.EJBException)228 Properties (java.util.Properties)213 Context (javax.naming.Context)194 RemoteException (java.rmi.RemoteException)173 TestFailureException (org.apache.openejb.test.TestFailureException)172 NamingException (javax.naming.NamingException)168 AssertionFailedError (junit.framework.AssertionFailedError)168 JMSException (javax.jms.JMSException)167 RemoveException (javax.ejb.RemoveException)66 CreateException (javax.ejb.CreateException)57 DataSource (javax.sql.DataSource)55 Hashtable (java.util.Hashtable)54 Assembler (org.apache.openejb.assembler.classic.Assembler)47 EjbJar (org.apache.openejb.jee.EjbJar)41 NameNotFoundException (javax.naming.NameNotFoundException)40 ConfigurationFactory (org.apache.openejb.config.ConfigurationFactory)38 Connection (java.sql.Connection)37 StatelessBean (org.apache.openejb.jee.StatelessBean)30