Spring Using Redis #Cacheable - java

Can any one suggest a Redis (NoSQL DB) example for a Collection type?
Generally we use the following (in Spring):
#Cacheable(value = "PRODUCT", key = "#productId" )
public Map<String,Object> findProduct(String productId, String productName)
{ return map; }
which stores the key and value as String, but I need for:
public Map<RestaurantId,Set<Order>>find(String RestaurantId, String productName){ return map; }

Make sure your objects are Serializable, and then simply use the Spring Data Redis cache abstraction (2015 link)
Example copied from the documentation above:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:use-pool="true"/>
<!-- redis template definition -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
p:connection-factory-ref="jedisConnectionFactory"/>
<!-- turn on declarative caching -->
<cache:annotation-driven />
<!-- declare Redis Cache Manager -->
<bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager" c:template-ref="redisTemplate"/>
</beans>

You could use Redisson Redis Java client which provides Spring Cache manager as well. It supports both TTL and maxIdleTime parameters.
Code example:
#Configuration
#ComponentScan
#EnableCaching
public static class Application {
#Bean(destroyMethod="shutdown")
RedissonClient redisson() throws IOException {
Config config = new Config();
config.useClusterServers()
.addNodeAddress("redis://127.0.0.1:7004", "redis://127.0.0.1:7001");
return Redisson.create(config);
}
#Bean
CacheManager cacheManager(RedissonClient redissonClient) {
Map<String, CacheConfig> config = new HashMap<String, CacheConfig>();
// create "testMap" cache with ttl = 24 minutes and maxIdleTime = 12 minutes
config.put("testMap", new CacheConfig(24*60*1000, 12*60*1000));
return new RedissonSpringCacheManager(redissonClient, config);
}
}

Related

XML to Spring Java annotation conversion

I have a sample code in which I am trying to convert application-context.xml to Spring annotated Java class. How to add constructor-arg with proper annotations here. Could you please help me sort this.
application-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:oxm="http://www.springframework.org/schema/oxm" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<context:component-scan base-package="myproject"/>
<!-- Define the SOAP version used by the WSDL -->
<bean id="soapMessageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory">
<property name="soapVersion">
<util:constant static-field="org.springframework.ws.soap.SoapVersion.SOAP_12"/>
</property>
</bean>
<!-- The location of the generated Java files -->
<oxm:jaxb2-marshaller id="marshaller" contextPath="myproject.wsdl.currency"/>
<!-- Configure Spring Web Services -->
<bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
<constructor-arg ref="soapMessageFactory"/>
<property name="marshaller" ref="marshaller"/>
<property name="unmarshaller" ref="marshaller"/>
<property name="defaultUri" value="http://www.webservicex.net/CurrencyConvertor.asmx?WSDL"/>
</bean>
<bean id="example" class="org.project.Example"/>
I have created Config.class as below.
#Configuration
public class AppConfig {
#Bean(name="soapMessageFactory")
public SaajSoapMessageFactory getSoapMsgFactory() {
SaajSoapMessageFactory soapFactory = new SaajSoapMessageFactory();
soapFactory.setSoapVersion(SoapVersion.11);
return soapFactory;
}
#Bean(name="webServiceTemplate")
public WebServiceTemplate getWsTemplate(Marshaller marshaller, Unmarshaller unmarshaller) {
WebServiceTemplate wsTemplate = new WebServiceTemplate();
//how to configure the constructor-arg here
wsTemplate.setMarshaller(marshaller);
wsTemplate.setUnMarshaller(unmarshaller);
wsTemplate.setDefaultUri(http://www.webservicex.net/CurrencyConvertor.asmx?WSDL);
return wsTemplate;
}
#Bean(name="example")
public Example getExample() {
return new ExampleImpl();
}
This should work...
#Bean(name="soapMessageFactory")
public SaajSoapMessageFactory getSoapMsgFactory() {
SaajSoapMessageFactory soapFactory = new SaajSoapMessageFactory();
soapFactory.setSoapVersion(SoapVersion.11);
return soapFactory;
}
#Bean(name="webServiceTemplate")
public WebServiceTemplate getWsTemplate(SaajSoapMessageFactory soapFactory, Marshaller marshaller, Unmarshaller unmarshaller) {
WebServiceTemplate wsTemplate = new WebServiceTemplate(soapFactory);
wsTemplate.setMarshaller(marshaller);
wsTemplate.setUnMarshaller(unmarshaller);
wsTemplate.setDefaultUri(http://www.webservicex.net/CurrencyConvertor.asmx?WSDL);
return wsTemplate;
}
When you specify any dependecy in #Bean method, spring will search for that bean in its context.
Since SaajSoapMessageFactory is locally created and doesn't have any other dependency, you could just call it locally like this
WebServiceTemplate wsTemplate = new WebServiceTemplate(getSoapMsgFactory());

Bean Creation Exception: Could not resolve matching constructor while loading xml

I am trying to call mainframe stored procedure using TempStatusClass in this class I am loading status-dao.xml which has the datasource defined and stored procedure defined. When I try to call this I am getting following exception org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testProcedure' defined in class path resource [status-dao.xml]: Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities).
Not sure what is causing this. Any help is highly appreciated.
#TempStatusClass
public class TempStatusClass implements DataServiceIF{
#Override
public Object execute(Object param) throws AppException {
StatusUpdateVO input = new StatusUpdateVO();
input.setShipment("X3328332842");
Map dataMap = null;
String springConfig = "status-dao.xml";
ApplicationContext context =new ClassPathXmlApplicationContext(springConfig);
StatusUpdateImpl statusUpdate = (StatusUpdateImpl) context.getBean("statusUpdateDao");
try {
dataMap = statusUpdate.getData(input);
} catch (StatusUpdateDAOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return dataMap;
}
}
XML
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<context:component-scan base-package="com.ops.test.test.*" />
<!-- Step 1: Define the data source -->
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/MAINEFRAME" />
</bean>
<!-- Step 2: Define JDBC template -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- Step 3: Define Stored Procedures -->
<bean id="testProcedure"
class="com.ops.test.test.sp.StatusUpdateStoredProcedure">
<constructor-arg>
<ref bean="jdbcTemplate" />
</constructor-arg>
<constructor-arg value="D472J00.N472RPTL" />
</bean>
<!-- Step 4: Define the DAOs -->
<bean id="statusUpdateDao"
class="com.ops.test.test.dao.impl.StatusUpdateImpl">
<property name="dataSource" ref="dataSource" />
<property name="procedure" ref="testProcedure" />
</bean>
</beans>
Impl
public class StatusUpdateImpl extends JdbcDaoSupport implements StatusUpdateDao{
/** The data source. */
private DataSource dataSource;
/** The stored procedure. */
private StatusUpdateDao storedProcedure;
/** The jdbc template object. */
#Autowired
private JdbcTemplate jdbcTemplateObject;
public StatusUpdateImpl()
{
}
#Autowired
public StatusUpdateImpl(DataSource dataSource) {
setDataSource(dataSource);
JdbcTemplate jdbcTemplate = getJdbcTemplate();
}
public void setStoredProcedure(StatusUpdateDao storedProcedure)
{
this.storedProcedure = storedProcedure;
}
public Map getData(Object input) throws StatusUpdateDAOException {
Map data = storedProcedure.getData(input);
return data;
}
}
Are you using spring-data-commons RC1 version? I have the same problem and it started with version bump of this dependency. I created an issue on spring's jira as well:
https://jira.spring.io/browse/DATASOLR-348

spring integration with activemq

I'm utilizing spring for about a year and everything was pretty obvious and simple until I faced spring integration. Ashamed, I can't create Outbound Gateway to send message to remote JMS (ActiveMQ) channel. Before integration I just used JmsTemplates and #JmsListeners directly, there were no problems at all.
What's the difference between request-destination and request-channel?
Here are my configs:
#Configuration
#EnableJms
public class JmsConfig {
...
#Value("${activemq.url}")
private String brokerUrl;
#Bean
public JmsTemplate jmsTemplate() {
JmsTemplate jmsTemplate = new JmsTemplate(queueConnectionFactory());
jmsTemplate.setDefaultDestinationName(bookingChannelName);
return jmsTemplate;
}
/*
#Bean(name = "bookingChannel")
public Queue activeMQQueue() {
return new ActiveMQQueue(bookingChannelName);
}
*/
#Bean
public JmsListenerContainerFactory jmsListenerContainerFactory() {
SimpleJmsListenerContainerFactory listenerFactory = new SimpleJmsListenerContainerFactory();
listenerFactory.setConnectionFactory(queueConnectionFactory());
return listenerFactory;
}
...
}
and an xml one, where all I've got in root beans element is:
<jms:outbound-gateway request-destination-name="reqDestination" request-channel="bookingChannel" />
Then, there is a gateway code:
#MessagingGateway
public interface BookingGateway<T> {
#Gateway
void bookTicket(T ticket);
}
And finally, here is how I use a gateway:
#Component
public class BookingGatewayImpl<T> {
#Autowired
private BookingGateway bookingGateway;
public <U> void bookTicket(T ticket, BiConsumer<T, U> onStatusReceived) {
bookingGateway.bookTicket(ticket); // second param is not utilized yet
}
}
And when ticket is about to be booked, I get:
send is not supported, because no request channel has been configured
Also, I am not able to uncomment ActiveMQQueue bean from the first listing because spring says it is not compatible with MessageChannel:
Bean named 'bookingChannel' must be of type [org.springframework.messaging.MessageChannel], but was actually of type [org.apache.activemq.command.ActiveMQQueue]
Why oh why should the destination be of MessageChannel type? What exactly am I doing wrong and how to get this ticket message to be sent?
bookingChannel is a MessageChannel between your MessagingGateway and the jms gateway. The destination is the AMQP Queue.
Use
#Gateway(requestChannel="bookingChannel")
#Bean(name = "bookingDestination")
public Queue activeMQQueue() {
return new ActiveMQQueue(bookingChannelName);
}
#Bean(name = "bookingChannel")
public MessageChannel bookingChannel() {
return new DirectChannel();
}
<jms:outbound-gateway request-destination-name="bookingDestination" request-channel="bookingChannel" />
Finally, came up with following configuration. Almost understood the way it's working now:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:jms="http://www.springframework.org/schema/integration/jms"
xmlns:id="http://www.springframework.org/schema/integration"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd http://www.springframework.org/schema/integration/jms http://www.springframework.org/schema/integration/jms/spring-integration-jms.xsd">
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="nio://127.0.0.1:61616"/>
</bean>
<bean id="requestQueue" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="bookingChannel"/>
</bean>
<id:channel id="requestChannel"/>
<bean id="channelInterceptor" class="c.e.mentoring.integration.gateway.RequestChannelInterceptor"/>
<jms:outbound-channel-adapter channel="requestChannel" destination="requestQueue"/>
<int:channel-interceptor ref="channelInterceptor"/>
</beans>

Spring integration mail inbound channel

i'm new to Spring and Spring integration, and i have a simple task to accomplish. Filtering some emails by it's subject though a regex and register some info in the db.
I've set the JavaMailProperties and the test gives me the output of the read emails but the method i'm setting with service-activator is never called and this is actually making me have a strong headache.
The following is the xml configuration file:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mail="http://www.springframework.org/schema/integration/mail"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/integration/mail
http://www.springframework.org/schema/integration/mail/spring-integration-mail-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<util:properties id="javaMailProperties">
<prop key="mail.store.protocol">pop3</prop>
<prop key="mail.debug">true</prop>
</util:properties>
<mail:inbound-channel-adapter id="pop3Adapter"
store-uri="pop3://username:password#mail..example.com:110/INBOX"
channel="recieveEmailChannel"
should-delete-messages="false"
auto-startup="true"
java-mail-properties="javaMailProperties"
mail-filter-expression="subject matches '(^Presente+\\s([1-9]{1})+(\\s[-]\\s)+([A-Z]{4,})+(\\s[A-Z]{6,})$)'">
<int:poller max-messages-per-poll="10" fixed-delay="10000"/>
</mail:inbound-channel-adapter>
<int:channel id="recieveEmailChannel">
<int:interceptors>
<int:wire-tap channel="logger"/>
</int:interceptors>
</int:channel>
<int:logging-channel-adapter id="logger" level="DEBUG"/>
<int:service-activator input-channel="recieveEmailChannel" ref="leggiMail" method="processa_mail"/>
<bean id="leggiMail" class="it.jenia.ac.mail.rapportini.LeggiMail">
</bean>
</beans>
The LeggiMail class with the processa_mail method is very simple:
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.stereotype.Service;
#Service
public class LeggiMail {
private static Logger logger = Logger.getLogger(LeggiMail.class);
public static int count_get = 0;
#ServiceActivator
public void processa_mail(MimeMessage mimeMessage) {
count_get++;
logger.debug("porcessa_mail working");
}
The test class i'm using this application in:
#ContextConfiguration(locations = { "classpath*:/test-spring-configuration.xml" })
#RunWith(SpringJUnit4ClassRunner.class)
#TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, TransactionalTestExecutionListener.class })
#TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false)
public class LeggiMailTest {
private static Logger logger = Logger.getLogger(LeggiMailTest.class);
#Autowired
LeggiMail lm;
#Test
#Transactional(value = "transactionManager", propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = Exception.class)
public void test_processa_mail(){
logger.debug("Test started");
}
}
The log Test started appears correctly in the console, but the log porcessa_mail working never shows up..
The first tutorial i found on this subject just spoke about a method that would have been called by default by the context. http://blog.solidcraft.eu/2011/04/read-emails-from-imap-with-spring.html (And it says that the method "processa_mail" should be called by default when the context is loaded, cause it's a service-activator.
Reading this tutorial about service activator didn't help enough: http://docs.spring.io/spring-integration/reference/html/messaging-endpoints-chapter.html
When you try to test some async stuff you some barrier to prevent the main thread to be stopped early, than it is neccessary for entire test-case.
The simples way is add Thread.sleep() before the end of test method.
But better solution is based on some synchonizer, like CountDonwLatch.
From other side there is QueueChannel in the Spring Integration. You can use it as an output-channel for <service-activator>. Inject it to the test class and wait on the output.receive(10000). And assert... the result message to make your test realy unit-test.

SessionFactory not initialized

i created a DAO (ForecastPersistorDao.java) which is the interface and its corresponding implementation (ForecastPersistorDaoImpl.java). The implementation has a method 'persist()' whose job is to persist some data into the database. When I try to call persist to persist the data, it throws a NullPointerException since it does not initialize SessionFactory properly. Please help me point out what I might be doing wrong:
ForecastPersistorDao.java
public interface ForecastPersistorDao {
void persist(List<ForecastedDemand> forecastedDemands);
List<DemandForecast> retrieveLastForecast(String marketplaceId);
}
ForecastPersistorDaoImpl.java
#Repository("forecastPersistorDao")
public class ForecastPersistorDaoImpl implements ForecastPersistorDao {
private SessionFactory sessionFactory;
#Autowired
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
/**
* Persist forecast in the database for later
*
* #param forecastedDemands
* List of forecast for all asin:marketplaceId tuple
*/
#Transactional
#Override
public void persist(List<ForecastedDemand> forecastedDemands) {
System.out.println("THIS IS ALWAYS NULL-------->>>>>>>> " + sessionFactory);
Session session = sessionFactory.getCurrentSession();
Date forecastCalculationDate = new Date();
// For each [asin:marketplace] tuple
for (ForecastedDemand forecastedDemand : forecastedDemands) {
String asin = forecastedDemand.getAsinMarketplaceId().getAsin();
String marketplaceId = forecastedDemand.getAsinMarketplaceId().getMarketplaceId();
String forecastingModel = forecastedDemand.getForecastingModel();
SortedMap<Instant, Double> forecast = forecastedDemand.getForecast();
// for each forecast date - write an entry in demand_forecasts table
for (Map.Entry<Instant, Double> entry : forecast.entrySet()) {
Date dateOfForecast = entry.getKey().toDate();
double quantityForecasted = entry.getValue();
DemandForecast forecastToPersist = new DemandForecast(asin, marketplaceId, forecastCalculationDate,
forecastingModel, quantityForecasted, dateOfForecast);
session.save(forecastToPersist);
}
}
}
Main class (Runner.java):
public final class FbsRunner {
private ForecastPersistorDao forecastPersistorDao;
public static void main(String[] args) {
Runner runner = new Runner();
runner.run();
}
public void run() {
ApplicationContext context =
new FileSystemXmlApplicationContext("spring-configuration/application-config.xml");
forecastPersistorDao = (ForecastPersistorDao) context.getBean("forecastPersistorDao"); // this works fine
System.out.println(">>>>>>>>>> forecastPersistorDao [this is fine (not null)]: " + forecastPersistorDao);
List<ForecastedDemand> forecastedDemand = [a list of Forecasted demand to be persisted int he DB]
// THE CALL BELOW FAILS...
forecastPersistorDao.persist(forecastedDemands);
System.out.println("Persisted demand in the database"); // We don't reach here.
}
}
spring-configuration/application-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
">
<!-- The main application context spring configuration -->
<import resource="application/hibernate.xml" />
<import resource="common/hibernate.xml" />
<!--<import resource="application/proxies.xml" />-->
<!--<import resource="common/aggregators.xml" /> -->
<import resource="application/environment.xml" />
<!--
Add any beans specific to your application here
-->
<bean id="forecastPersistorDao" class="com.amazon.fresh.fbs.dao.ForecastPersistorDaoImpl" />
</beans>
application/hibernate.xml:
<bean id="SessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
parent="AbstractSessionFactory" depends-on="EnvironmentHelper" >
<property name="hibernateProperties">
...
...
everything as expected
...
</bean>
common/hibernate.xml:
<beans ... >
bean id="AbstractSessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
abstract="true">
<property name="mappingResources">
<list>
<value>com/amazon/fresh/fbs/dao/hibernate/RtipData.hbm.xml</value>
<value>com/amazon/fresh/fbs/dao/hibernate/Vendor.hbm.xml</value>
<value>com/amazon/fresh/fbs/dao/hibernate/AdjustmentPeriod.hbm.xml</value>
<value>com/amazon/fresh/fbs/dao/hibernate/DemandForecast.hbm.xml</value>
</list>
</property>
<property name="exposeTransactionAwareSessionFactory">
<value>true</value>
</property>
</bean>
<!-- Use Spring transactions for Hibernate -->
<tx:annotation-driven transaction-manager="txManager" mode='proxy' proxy-target-class='true'/>
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="SessionFactory" />
</bean>
<aop:aspectj-autoproxy/>
</beans>
Please try to add <context:component-scan base-package="YOUR PACKAGE NAME" /> into your application-config.xml. This will tell Spring to scan for annotated components that will be auto-registered as Spring beans.
"YOUR PACKAGE NAME" should be the package name to scan for annotated components.
sessionFactory didn't autowired correctly ,add "#component" for ForecastPersistorDaoImpl and add "context:component-scan" in application-config.xml to tell spring to initialize.

Categories

Resources