Search in sources :

Example 51 with Map

use of java.util.Map in project camel by apache.

the class InfinispanProducerTest method publishMapWithLifespanAsync.

@Test
public void publishMapWithLifespanAsync() throws Exception {
    template.send("direct:putallasync", new Processor() {

        @Override
        public void process(Exchange exchange) throws Exception {
            Map<String, String> map = new HashMap<String, String>();
            map.put(KEY_ONE, VALUE_ONE);
            map.put(KEY_TWO, VALUE_TWO);
            exchange.getIn().setHeader(InfinispanConstants.MAP, map);
            exchange.getIn().setHeader(InfinispanConstants.OPERATION, InfinispanConstants.PUT_ALL);
            exchange.getIn().setHeader(InfinispanConstants.LIFESPAN_TIME, new Long(LIFESPAN_TIME));
            exchange.getIn().setHeader(InfinispanConstants.LIFESPAN_TIME_UNIT, TimeUnit.MILLISECONDS.toString());
        }
    });
    waitFor(new Condition() {

        @Override
        public boolean isSatisfied() throws Exception {
            Object valueOne = currentCache().get(KEY_ONE);
            Object valueTwo = currentCache().get(KEY_TWO);
            return valueOne.equals(VALUE_ONE) && valueTwo.equals(VALUE_TWO) && currentCache().size() == 2;
        }
    }, 100);
    waitForNullValue(KEY_ONE);
}
Also used : Exchange(org.apache.camel.Exchange) Condition(org.apache.camel.component.infinispan.util.Condition) Processor(org.apache.camel.Processor) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 52 with Map

use of java.util.Map in project camel by apache.

the class InfinispanProducerTest method publishMapWithLifespanAndMaxIdleTime.

@Test
public void publishMapWithLifespanAndMaxIdleTime() throws Exception {
    template.send("direct:start", new Processor() {

        @Override
        public void process(Exchange exchange) throws Exception {
            Map<String, String> map = new HashMap<String, String>();
            map.put(KEY_ONE, VALUE_ONE);
            map.put(KEY_TWO, VALUE_TWO);
            exchange.getIn().setHeader(InfinispanConstants.MAP, map);
            exchange.getIn().setHeader(InfinispanConstants.OPERATION, InfinispanConstants.PUT_ALL);
            exchange.getIn().setHeader(InfinispanConstants.LIFESPAN_TIME, new Long(LIFESPAN_FOR_MAX_IDLE));
            exchange.getIn().setHeader(InfinispanConstants.LIFESPAN_TIME_UNIT, TimeUnit.MILLISECONDS.toString());
            exchange.getIn().setHeader(InfinispanConstants.MAX_IDLE_TIME, new Long(MAX_IDLE_TIME));
            exchange.getIn().setHeader(InfinispanConstants.MAX_IDLE_TIME_UNIT, TimeUnit.MILLISECONDS.toString());
        }
    });
    assertEquals(2, currentCache().size());
    Thread.sleep(300);
    waitForNullValue(KEY_TWO);
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 53 with Map

use of java.util.Map in project camel by apache.

the class DefaultJdbcPrepareStatementStrategy method createPopulateIterator.

@Override
public Iterator<?> createPopulateIterator(final String query, final String preparedQuery, final int expectedParams, final Exchange exchange, final Object value) throws SQLException {
    Map<?, ?> map = null;
    if (exchange.getIn().hasHeaders()) {
        if (exchange.getIn().getHeader(JdbcConstants.JDBC_PARAMETERS) != null) {
            // header JDBC_PARAMETERS takes precedence over regular headers
            map = exchange.getIn().getHeader(JdbcConstants.JDBC_PARAMETERS, Map.class);
        } else {
            map = exchange.getIn().getHeaders();
        }
    }
    final Map<?, ?> headerMap = map;
    if (hasNamedParameters(query)) {
        // create an iterator that returns the value in the named order
        try {
            return new Iterator<Object>() {

                private NamedQueryParser parser = new NamedQueryParser(query);

                private Object next;

                private boolean done;

                private boolean preFetched;

                @Override
                public boolean hasNext() {
                    if (!done && !preFetched) {
                        next();
                        preFetched = true;
                    }
                    return !done;
                }

                @Override
                public Object next() {
                    if (!preFetched) {
                        String key = parser.next();
                        if (key == null) {
                            done = true;
                            return null;
                        }
                        // the key is expected to exist, if not report so end user can see this
                        boolean contains = headerMap != null && headerMap.containsKey(key);
                        if (!contains) {
                            throw new RuntimeExchangeException("Cannot find key [" + key + "] in message body or headers to use when setting named parameter in query [" + query + "]", exchange);
                        }
                        next = headerMap.get(key);
                    }
                    preFetched = false;
                    return next;
                }

                @Override
                public void remove() {
                // noop
                }
            };
        } catch (Exception e) {
            throw new SQLException("Error iterating parameters for the query: " + query, e);
        }
    } else {
        // just use a regular iterator
        return exchange.getContext().getTypeConverter().convertTo(Iterator.class, headerMap != null ? headerMap.values() : null);
    }
}
Also used : RuntimeExchangeException(org.apache.camel.RuntimeExchangeException) SQLException(java.sql.SQLException) Iterator(java.util.Iterator) Map(java.util.Map) SQLException(java.sql.SQLException) RuntimeExchangeException(org.apache.camel.RuntimeExchangeException)

Example 54 with Map

use of java.util.Map in project camel by apache.

the class AbstractJdbcGeneratedKeysTest method testRetrieveGeneratedKeysWithStringGeneratedColumns.

@SuppressWarnings("unchecked")
protected void testRetrieveGeneratedKeysWithStringGeneratedColumns(String query, Map<String, Object> parameters) throws Exception {
    // first we create our exchange using the endpoint
    Endpoint endpoint = context.getEndpoint("direct:hello");
    Exchange exchange = endpoint.createExchange();
    // then we set the SQL on the in body and add possible parameters
    exchange.getIn().setBody(query);
    exchange.getIn().setHeader(JdbcConstants.JDBC_RETRIEVE_GENERATED_KEYS, true);
    exchange.getIn().setHeader(JdbcConstants.JDBC_GENERATED_COLUMNS, new String[] { "ID" });
    setHeaders(exchange, parameters);
    // now we send the exchange to the endpoint, and receives the response from Camel
    Exchange out = template.send(endpoint, exchange);
    // assertions of the response
    assertNotNull(out);
    assertNotNull(out.getOut());
    assertNotNull(out.getOut().getHeader(JdbcConstants.JDBC_GENERATED_KEYS_DATA));
    assertNotNull(out.getOut().getHeader(JdbcConstants.JDBC_GENERATED_KEYS_ROW_COUNT));
    List<Map<String, Object>> generatedKeys = out.getOut().getHeader(JdbcConstants.JDBC_GENERATED_KEYS_DATA, List.class);
    assertNotNull("out body could not be converted to an ArrayList - was: " + out.getOut().getBody(), generatedKeys);
    assertEquals(1, generatedKeys.size());
    Map<String, Object> row = generatedKeys.get(0);
    assertEquals("auto increment value should be 2", BigDecimal.valueOf(2), row.get("1"));
    assertEquals("generated keys row count should be one", 1, out.getOut().getHeader(JdbcConstants.JDBC_GENERATED_KEYS_ROW_COUNT));
}
Also used : Exchange(org.apache.camel.Exchange) Endpoint(org.apache.camel.Endpoint) Map(java.util.Map)

Example 55 with Map

use of java.util.Map in project camel by apache.

the class AbstractJdbcGeneratedKeysTest method testRetrieveGeneratedKeys.

@SuppressWarnings("unchecked")
protected void testRetrieveGeneratedKeys(String query, Map<String, Object> parameters) throws Exception {
    // first we create our exchange using the endpoint
    Endpoint endpoint = context.getEndpoint("direct:hello");
    Exchange exchange = endpoint.createExchange();
    // then we set the SQL on the in body and add possible parameters
    exchange.getIn().setBody(query);
    exchange.getIn().setHeader(JdbcConstants.JDBC_RETRIEVE_GENERATED_KEYS, true);
    setHeaders(exchange, parameters);
    // now we send the exchange to the endpoint, and receives the response from Camel
    Exchange out = template.send(endpoint, exchange);
    // assertions of the response
    assertNotNull(out);
    assertNotNull(out.getOut());
    assertNotNull(out.getOut().getHeader(JdbcConstants.JDBC_GENERATED_KEYS_DATA));
    assertNotNull(out.getOut().getHeader(JdbcConstants.JDBC_GENERATED_KEYS_ROW_COUNT));
    List<Map<String, Object>> generatedKeys = out.getOut().getHeader(JdbcConstants.JDBC_GENERATED_KEYS_DATA, List.class);
    assertNotNull("out body could not be converted to an ArrayList - was: " + out.getOut().getBody(), generatedKeys);
    assertEquals(1, generatedKeys.size());
    Map<String, Object> row = generatedKeys.get(0);
    assertEquals("auto increment value should be 2", BigDecimal.valueOf(2), row.get("1"));
    assertEquals("generated keys row count should be one", 1, out.getOut().getHeader(JdbcConstants.JDBC_GENERATED_KEYS_ROW_COUNT));
}
Also used : Exchange(org.apache.camel.Exchange) Endpoint(org.apache.camel.Endpoint) Map(java.util.Map)

Aggregations

Map (java.util.Map)15646 HashMap (java.util.HashMap)9529 ArrayList (java.util.ArrayList)3619 List (java.util.List)2988 Test (org.junit.Test)2558 Set (java.util.Set)1837 HashSet (java.util.HashSet)1646 IOException (java.io.IOException)1486 Iterator (java.util.Iterator)1307 LinkedHashMap (java.util.LinkedHashMap)1284 TreeMap (java.util.TreeMap)1022 ImmutableMap (com.google.common.collect.ImmutableMap)879 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)729 File (java.io.File)662 Collection (java.util.Collection)576 Collectors (java.util.stream.Collectors)436 ConcurrentMap (java.util.concurrent.ConcurrentMap)375 LinkedList (java.util.LinkedList)333 SSOException (com.iplanet.sso.SSOException)294 Collections (java.util.Collections)288