How to get the current info of the JedisPool - spring

When I use spring-data-redis inject RedisConnectionFactory and setup the poolConfig, how can I get the infomation of the pool in runtime, because I found the factory does not expose the pool. Thank you advance.^.^
my code is :
#Bean
public RedisConnectionFactory jedisConnectionFactory() {
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxIdle(128);
poolConfig.setJmxEnabled(true);
poolConfig.setMaxTotal(8);
poolConfig.setMinIdle(2);
JedisConnectionFactory factory = new JedisConnectionFactory(poolConfig);
factory.setHostName("localhost");
factory.setPort(6379);
factory.setUsePool(true);
return factory;
}

Related

Redelivery Policy Is Not Working in Activemq Spring Boot

I have used below configuration
#SpringBootApplication
#EnableScheduling
public class NotificationApplication {
#Value("${jms.broker.endpoint}")
private String brokerUrl;
#Autowired
private Environment env;
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(NotificationApplication.class, args);
RecordReader abc = ctx.getBean(RecordReader.class);
abc.readNotifications();
}
#Bean
public ActiveMQConnectionFactory activeMQConnectionFactory() {
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(brokerUrl);
RedeliveryPolicy redeliveryPolicy = new RedeliveryPolicy();
redeliveryPolicy.setMaximumRedeliveries(env.getProperty("jms.redelivery.maximum", Integer.class));
factory.setRedeliveryPolicy(redeliveryPolicy);
factory.setTrustedPackages(Arrays.asList("com.lms.notification"));
return factory;
}
#Bean
public DefaultJmsListenerContainerFactory jmsListenerContainerFactory() throws Throwable {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setConnectionFactory(activeMQConnectionFactory());
factory.setMessageConverter(jacksonJmsMessageConverter());
factory.setConcurrency(env.getProperty("jms.connections.concurrent"));
return factory;
}
#Bean
public JmsTemplate jmsTemplate() {
JmsTemplate template = new JmsTemplate();
template.setConnectionFactory(activeMQConnectionFactory());
template.setMessageConverter(jacksonJmsMessageConverter());
return template;
}
#Bean
public MessageConverter jacksonJmsMessageConverter() {
MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
converter.setTargetType(MessageType.TEXT);
converter.setTypeIdPropertyName("_type");
return converter;
}
}
The issue is the Redelivery policy is not working as I have defined in activeMQConnectionFactory bean. Means I have set maximum redelivery 1, but is not being redelivered in case of exception in listener. Also in case of exception in listener it should go to the default DLQ, which is also not happening. But if I comment the jmsListenerContainerFactory bean all works fine.
I am not able to identify why this is happening. Can any one look into this what wrong I am doing?
I am using Activemq 5.16.1
Thanks

How to using annotation #RefreshScope with spingboot : connectionFactory?

how to refresh connectionFactory or connectionpool,such as dataSource,JedisConnectionFactory... when the springboot application is running?
(I used springcould-config to update my configs)
#Bean
#ConfigurationProperties(prefix = "spring.redis.pool")
public JedisPoolConfig getRedisConfig() {
JedisPoolConfig config = new JedisPoolConfig();
return config;
}
#Bean(name = "redisConnection")
public JedisConnectionFactory getConnectionFactory() {
JedisConnectionFactory factory = new JedisConnectionFactory();
JedisPoolConfig config = getRedisConfig();
factory.setPoolConfig(config);
factory.setPassword(password);
factory.setHostName(hostName);
factory.setPort(port);
factory.setDatabase(database);
return factory;
}
...
pseudocode:
onChangeListener function then
refreshScope.refresh("redisConnection");
but it does not work,however, some files with #Value("${spring.redis.host}")
it change

Could not find default ScheduledExecutorService bean with Redis and Spring

I have just develop a configuration with Spring Session and Redis, everything work fine, but in my console logs, I got
2015-06-29 15:45:44,088 [main] DEBUG org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor - Could not find default ScheduledExecutorService bean
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.scheduling.TaskScheduler] is defined
How can I configure ScheduledExecutorService bean?
Update :
#Configuration
#EnableRedisHttpSession
#Conditional(RedisDeclarationCondition.class)
public class LocalRedisConfig extends WebMVCConfig{
#Value("${redis.host}")
private String host;
#Value("${redis.port}")
private String port;
#Bean
public JedisConnectionFactory connectionFactory() {
return new JedisConnectionFactory();
}
#Bean
public RedisConnectionFactory jedisConnectionFactory(){
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxIdle(5);
poolConfig.setMaxTotal(10);
poolConfig.setMinIdle(1);
poolConfig.setTestOnBorrow(true);
poolConfig.setTestOnReturn(true);
poolConfig.setTestWhileIdle(true);
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(poolConfig);
// RedisOperationsSessionRepository cleanup = new RedisOperationsSessionRepository(jedisConnectionFactory);
//optional
//jedisConnectionFactory.setHostName(host);
//jedisConnectionFactory.setPort(Integer.valueOf(port));
return jedisConnectionFactory;
}
#Bean
public StringRedisTemplate redisTemplate(){
StringRedisTemplate redisTemplate = new StringRedisTemplate(jedisConnectionFactory());
return redisTemplate;
}
it is a DEBUG message, could be ignored by set your logging.level.org.springframework=INFO
Using #Scheduled and #EnableScheduling but gives NoSuchBeanDefinitionException

Change JMS settings with Glassfish and Spring

In our project we use Glassfish v3.1.2.2 with a ConnectionFactory bound as "jms/ConnectionFactory" and a Queue bound as "jms/Queue". Both are created in the glassfish admin console:
The Spring config is implemented this way:
#Bean
public JndiTemplate jndiTemplate() {
JndiTemplate jndiTemplate = new JndiTemplate();
Properties props = new Properties();
props.setProperty("java.naming.factory.initial", "com.sun.enterprise.naming.SerialInitContextFactory");
props.setProperty("java.naming.factory.url.pkgs", "com.sun.enterprise.naming");
props.setProperty("java.naming.factory.state", "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
props.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
props.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
jndiTemplate.setEnvironment(props);
return jndiTemplate;
}
#Bean
public JndiObjectFactoryBean connectionFactory() {
JndiObjectFactoryBean connectionFactory = new JndiObjectFactoryBean();
connectionFactory.setJndiTemplate(jndiTemplate());
connectionFactory.setJndiName("jms/ConnectionFactory");
return connectionFactory;
}
#Bean
public TransactionAwareConnectionFactoryProxy connectionFactoryProxy() {
return new TransactionAwareConnectionFactoryProxy((ConnectionFactory) connectionFactory().getObject());
}
#Bean
public JndiObjectFactoryBean destination() {
JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
jndiObjectFactoryBean.setJndiTemplate(jndiTemplate());
jndiObjectFactoryBean.setJndiName("jms/Queue");
return jndiObjectFactoryBean;
}
#Bean
public JmsTemplate jmsTemplate() {
JmsTemplate jmsTemplate = new JmsTemplate();
jmsTemplate.setConnectionFactory((ConnectionFactory) connectionFactory().getObject());
jmsTemplate.setDefaultDestination((Destination) destination().getObject());
return jmsTemplate;
}
#Bean
public DefaultMessageListenerContainer simpleMessageListenerContainer() {
DefaultMessageListenerContainer listenerContainer = new DefaultMessageListenerContainer();
listenerContainer.setConnectionFactory(connectionFactoryProxy());
listenerContainer.setDestination((Destination) destination().getObject());
listenerContainer.setMessageListener(messageDispatcher);
listenerContainer.setSessionTransacted(true);
listenerContainer.setTransactionManager(jtaTransactionManager());
return listenerContainer;
}
Everything works fine so far, message will be sent to and consumed from the queue without any problems. Rolling back messages wit a RuntimeException works, too.
The problem is, how to change some fundamental settings, like RedeliveryAttempts or RedeliveryInterval from the Activation Spec. I cant find any solution to change this with Spring only with MDB. Is there a way to do this in Glassfishs admin console or in Spring configuration? Did I have to use some different implementation or is it not possible over all?
Hope anybody can help.
Thanks in advance,
Danny
Through some trial and error I found the following, which works on Glassfish 4.
#Bean
public JmsTemplate jmsTemplate() {
JmsTemplate jmsTemplate = new JmsTemplate();
jmsTemplate.setPubSubDomain(true); //may be necessary if using topic
jmsTemplate.setDefaultDestinationName("topicname");
jmsTemplate.setConnectionFactory(connectionFactory());
return jmsTemplate;
}
#Bean
public ConnectionFactory connectionFactory() {
try {
JndiObjectFactoryBean jndiFactory = new JndiObjectFactoryBean();
jndiFactory.setJndiName("jms/ConnectionFactory");
jndiFactory.setResourceRef(true); //adds java:comp/env/ prefix
jndiFactory.afterPropertiesSet(); //very important, actually causes the object to be loaded
return (ConnectionFactory) jndiFactory.getObject();
} catch (IllegalArgumentException | NamingException e) {
throw new RuntimeException(e);
}
}

Cannot find the JBoss ConnectionFactory with a JNDI lookup in Spring annotation configuration

Despite what seems to be a successful binding of the JBoss (AS 7.1.1.Final) connection factory:
[org.jboss.as.messaging] (MSC service thread 1-9) JBAS011601: Bound messaging object to jndi name java:/ConnectionFactory
The ConnectionFactory in the lookup is always null. Can anyone see what the problem is?
#Configuration
#ComponentScan(basePackages = "reservation")
public class AppConfiguration extends WebMvcConfigurerAdapter {
// ***********************//
// ******** JMS **********//
// ***********************//
#Bean
public ConnectionFactory jmsConnectionFactory() {
JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
jndiObjectFactoryBean.setJndiName("java:/ConnectionFactory");
return (ConnectionFactory) jndiObjectFactoryBean.getObject();
}
#Bean
public Queue requestsQueue() {
JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
jndiObjectFactoryBean.setJndiName("java:/queue/test");
return (Queue) jndiObjectFactoryBean.getObject();
}
#Bean
public JmsOperations jmsOperations() {
final JmsTemplate jmsTemplate = new JmsTemplate(jmsConnectionFactory());
jmsTemplate.setDefaultDestination(requestsQueue());
return jmsTemplate;
}
}
Unfortunately you must call afterPropertiesSet() manually:
#Bean
public ConnectionFactory jmsConnectionFactory() {
JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
jndiObjectFactoryBean.setJndiName("java:/ConnectionFactory");
jndiObjectFactoryBean.afterPropertiesSet(); //HERE
return (ConnectionFactory) jndiObjectFactoryBean.getObject();
}
An alternative I particularly like is as follows:
#Bean
public JndiObjectFactoryBean jmsConnectionFactoryFactoryBean() {
JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
jndiObjectFactoryBean.setJndiName("java:/ConnectionFactory");
return jndiObjectFactoryBean;
}
public ConnectionFactory jmsConnectionFactory() {
return (ConnectionFactory) jmsConnectionFactoryFactoryBean().getObject();
}
Notice that jmsConnectionFactory() is not annotated with #Bean (it's important). In that case Spring will call appropriate callback method for you.

Resources