Grassfield Tamil Blog Aggregator

I’m happy to introduce the brand new blog aggregator www.grassfield.org. Today is the day 1 🙂

This aggregates Tamil and English blogs. This platform is running using my opensource project jAtomRss. I’m happy to come out with a website, finally.

Thanking Sivakumar and Kannan for their technical support and Sindu for testing the same!

Nexted exception is java.awt.HeadlessException

This is what I get when I start my SpringBoot application, built using Java Swing.

Exception encountered during context initialization. Constructor threw Exception java.awt.HeadlessException

Adding the headless JVM param would resolve this problem

-Djava.awt.headless=false

Uncaught Error: Cannot pass parameter 4 by reference

$qry = "insert into feed_info (site_url, feed_xml_url, enabled) values (?,?,?)";
$stmt->bind_param('sss', $obj["site_url"], $obj["feed_xml_url"],0);

This was the PHP code that threw this exception.

Uncaught Error: Cannot pass parameter 4 by reference

PHP error: “Cannot pass parameter 2 by reference” The pass by value 0 caused the issue, when the method is expecting a variable / pass by reference.

The following code fixed the problem.

$enabled=0;
$stmt->bind_param('sss', $obj["site_url"], $obj["feed_xml_url"],$enabled);

Jackson JSON Capitalization: Unrecognized field

Hi,

I need to covert the following snippet of JSON response to its relevant object. The key is ComponentType not componentType which didnt adhere to java variable standards.

{
"ComponentType": "APC_Ups",
"servers": [
"APC01"
]
}

Object coversion by Jackson failed with error com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field “ComponentType”

Jackson tries to search for the variable name as per java variable naming standards. As there is no variable defined as ‘componentType’, it fails. Defining right variable name using @JsonProperty will solve this problem!

@JsonProperty (“ComponentType”)
String ComponentType;

Ref: Jackson JSON field mapping capitalization?

Chennai T Nagar

Hide blocks in jQuery

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("p").click(function(){
    $(this).hide();
  });
  $("content").click(function(){
	$(this).hide();
  });
  $("#myid").click(function(){
	$(this).hide();
  });
  $(".myclass").click(function(){
	$(this).hide();
  });
});
</script>
</head>
<body>

<p>I am a paragraph</p>
<content>I am a content</content>
<ul id="myid">
<li>I am an unordered list identified as myid</li>
</ul>

<div class="myclass">
I am a div identified as myclass
</div>

</body>
</html>

The above snippet will help to hide the given blocks like in paragraph, content and unordered list blocks.

Initially it blocks everything. When we click any of the blocks, it will hide. After clicking the paragraph in the first line, this is what you see.

This helps to hide standard blocks using its tag or id or class names!

Sending and receiving messages using Apache ActiveMQ with Spring Boot

Hi,

This is the continuation of my previous post Apache ActiveMQ. We shall see how to send and receive messages with SpringBoot using this ActiveMQ server.

STS Project Setup

Goto File>New>Spring Starter Project. We shall take the project template from Spring.io.

STS project

Start a new SpringProject

Include the JMS dependency

Your new project will be created. I need to add junit4 separately.

Maven dependencies

This is my pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.7.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>org.grassfield</groupId>
	<artifactId>jmsdemo</artifactId>
	<version>1.0</version>
	<name>jmsdemo</name>
	<description>jmsdemo</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-activemq</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

Application.properties

Add the following property to Application.properties

activemq.broker-url: tcp://192.168.1.150:61616

If you didn’t see my earlier post, 192.168.1.150 is where my ActiveMQ server is running.

Sender code:

Sender is a simple POJO, that contains the methods to send a message.

Sender.java

package org.grassfield;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;

public class Sender {
	private static final Logger LOGGER = LoggerFactory.getLogger(Sender.class);
	@Autowired
	private JmsTemplate jmsTemplate;
	
	public void send(String message) {
	    LOGGER.info("sending message='{}'", message);
	    jmsTemplate.convertAndSend("myqueue.q", message);
	  }
}

SenderConfig.java

package org.grassfield;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.connection.CachingConnectionFactory;
import org.springframework.jms.core.JmsTemplate;

@Configuration
public class SenderConfig {
	Logger logger = LoggerFactory.getLogger(SenderConfig.class);
	@Value("${activemq.broker-url}")
	private String brokerUrl;
	
	public ActiveMQConnectionFactory senderActiveMQConnectionFactory() {
		logger.info("initializing ActiveMQConnectionFactory");
		ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();
		logger.info("Setting up broker "+brokerUrl);
		activeMQConnectionFactory.setBrokerURL(brokerUrl);
		return activeMQConnectionFactory;
	}
	
	@Bean
	public CachingConnectionFactory cachingConnectionFactory() {
		return new CachingConnectionFactory(senderActiveMQConnectionFactory());
	}
	
	@Bean
	public JmsTemplate jmsTemplate() {
		return new JmsTemplate(cachingConnectionFactory());
	}
	
	@Bean
	public Sender sender() {
		return new Sender();
	}
}

Receiver Code

This code will receives the message sent by the Sender.java

Receiver.java

package org.grassfield;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.annotation.JmsListener;

public class Receiver {
	Logger LOGGER = LoggerFactory.getLogger(Receiver.class);

	  @JmsListener(destination = "myqueue.q")
	  public void receive(String message) {
	    LOGGER.info("received message='{}'", message);
	  }
}

ReceiverConfig.java

package org.grassfield;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;

@Configuration
@EnableJms
public class ReceiverConfig {
	@Value("${activemq.broker-url}")
	private String brokerUrl;
	
	@Bean
	public ActiveMQConnectionFactory receiverActiveMQConnectionFactory() {
		ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();
		activeMQConnectionFactory.setBrokerURL(brokerUrl);
	    return activeMQConnectionFactory;
	}
	
	@Bean
	public DefaultJmsListenerContainerFactory jmsListenerContainerFactory() {
		DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
	    factory.setConnectionFactory(receiverActiveMQConnectionFactory());
	    return factory;
	}
	
	@Bean
	public Receiver receiver() {
		return new Receiver();
	}

}

Spring Boot Application

And finally, this is our Spring Boot Application.

package org.grassfield;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class JmsdemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(JmsdemoApplication.class, args);
	}

}

Unit test

This is the unit test.

package com.eg;

import org.grassfield.JmsdemoApplication;
import org.grassfield.Sender;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = JmsdemoApplication.class)
class JmsdemoApplicationTest {

	@Autowired
	private Sender sender;

	@Test
	public void testReceive() {
		sender.send("Hello Spring JMS ActiveMQ!");
	}

}

Execution

We shall execute the unit test cases by running the project as a maven build with test goal.

As you see above, the message is sent by the Sender. The same has been received by Receiver.

Queue views – Graphs
Topics view statistics

Hence the message is sent and received.

Thanks Spring JMS ActiveMQ Example -CodeNotFound

Apache ActiveMQ

This post talks about the simple steps to make Apache ActiveMQ up and running.

Firstly, the ActiveMQ has to be downloaded from apache site

After downloading it, Extract it to a folder. In my case it is C:\apache-activemq-5.15.12

Apache Active MQ

To start MQ process, Execute C:\apache-activemq-5.15.12\bin\win64\activemq.bat batch file.

To access the web console, navigate http://localhost:8161/index.html

Apache ActiveMQ

To get into the management console, click the ‘Manage ActiveMQ Broker’ link. It is protected link. Use username admin and password admin.

Apache ActiveMQ

You will find the links to track the queues from here

Apache ActiveMQ

Hence the ActiveMQ server is installed with default configuration.

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed

I am connecting to a SSL site. This is one of the most frequent exceptions seen by a java developer.

To get rid of this, download the cert from the site you want to connect. Just click the lock sign near the browser URL. It will help you to download/export the SSL certificate to .cer format.

Import the certificate to your java cacerts.

I used OpenJDK

d:\java\jdk\bin\keytool -import -noprompt -trustcacerts -alias grassfield -file D:\gfield_ssl_cert.cer -keystore cacerts -storepass changeit
Certificate was added to keystore

You can list the certs in your cacerts store. I do a grep/find to get my cerficate.

d:\java\jdk\bin\keytool -list -keystore cacerts -storepass changeit|find “grassfield”
grassfield, Nov 25, 2019, trustedCertEntry,

Spring application fails to load in STS 4.1

I have a spring web application which is running in Spring 5 (written in 3, later upgraded to 4 and 5 over the time). It was created in STS 2.x, later the workspace has been imported to STS 3.x without any problem. Though I have been using STS 4.1 for other SpringBoot projects, my old web application was still being developed in STS 3.x.

I imported the application to STS4.1 last week. The tomcat starts. But it does not load my application.

Oct 19, 2019 6:48:23 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version: Apache Tomcat/9.0.13
Oct 19, 2019 6:48:23 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built: Nov 2 2018 14:27:55 UTC
Oct 19, 2019 6:48:23 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number: 9.0.13.0
Oct 19, 2019 6:48:23 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name: Windows 10
Oct 19, 2019 6:48:23 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version: 10.0
Oct 19, 2019 6:48:23 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture: amd64
Oct 19, 2019 6:48:23 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home: C:\Program Files\Java\jdk1.8.0_181\jre
Oct 19, 2019 6:48:23 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version: 1.8.0_181-b13
Oct 19, 2019 6:48:23 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor: Oracle Corporation
Oct 19, 2019 6:48:23 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE: H:\Dev\apache-tomcat-9.0.13
Oct 19, 2019 6:48:23 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME: H:\Dev\apache-tomcat-9.0.13
Oct 19, 2019 6:48:23 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=H:\Dev\apache-tomcat-9.0.13
Oct 19, 2019 6:48:23 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=H:\Dev\apache-tomcat-9.0.13
Oct 19, 2019 6:48:23 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=H:\Dev\apache-tomcat-9.0.13\wtpwebapps
Oct 19, 2019 6:48:23 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=H:\Dev\apache-tomcat-9.0.13\endorsed
Oct 19, 2019 6:48:23 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=Cp1252
Oct 19, 2019 6:48:23 AM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: Loaded APR based Apache Tomcat Native library [1.2.18] using APR version [1.6.5].
Oct 19, 2019 6:48:23 AM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
Oct 19, 2019 6:48:23 AM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
Oct 19, 2019 6:48:23 AM org.apache.catalina.core.AprLifecycleListener initializeSSL
INFO: OpenSSL successfully initialized [OpenSSL 1.1.1 11 Sep 2018]
Oct 19, 2019 6:48:23 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Oct 19, 2019 6:48:24 AM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Oct 19, 2019 6:48:24 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8009"]
Oct 19, 2019 6:48:24 AM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Oct 19, 2019 6:48:24 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1674 ms
Oct 19, 2019 6:48:24 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service [Catalina]
Oct 19, 2019 6:48:24 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/9.0.13
Oct 19, 2019 6:48:24 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory [H:\Dev\apache-tomcat-9.0.13\webapps\docs]
Oct 19, 2019 6:48:24 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory [H:\Dev\apache-tomcat-9.0.13\webapps\docs] has finished in [22] ms
Oct 19, 2019 6:48:24 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory [H:\Dev\apache-tomcat-9.0.13\webapps\examples]
Oct 19, 2019 6:48:25 AM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: contextInitialized()
Oct 19, 2019 6:48:25 AM org.apache.catalina.core.ApplicationContext log
INFO: SessionListener: contextInitialized()
Oct 19, 2019 6:48:25 AM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: attributeAdded('StockTicker', 'async.Stockticker@302552ec')
Oct 19, 2019 6:48:25 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory [H:\Dev\apache-tomcat-9.0.13\webapps\examples] has finished in [534] ms
Oct 19, 2019 6:48:25 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory [H:\Dev\apache-tomcat-9.0.13\webapps\host-manager]
Oct 19, 2019 6:48:25 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory [H:\Dev\apache-tomcat-9.0.13\webapps\host-manager] has finished in [31] ms
Oct 19, 2019 6:48:25 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory [H:\Dev\apache-tomcat-9.0.13\webapps\manager]
Oct 19, 2019 6:48:25 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory [H:\Dev\apache-tomcat-9.0.13\webapps\manager] has finished in [32] ms
Oct 19, 2019 6:48:25 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory [H:\Dev\apache-tomcat-9.0.13\webapps\ROOT]
Oct 19, 2019 6:48:25 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory [H:\Dev\apache-tomcat-9.0.13\webapps\ROOT] has finished in [46] ms
Oct 19, 2019 6:48:25 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
Oct 19, 2019 6:48:25 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
Oct 19, 2019 6:48:25 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 1299 ms

As you see in the above log, Spring context never loaded. When I tried to load the application URL, it was throwing HTTP Status 404 – Not Found error.

I had faced this kind of problem in earlier version of STS/Eclipse. It would be resolved either by tomcat server configuration or maven update or rebuilding or restart of eclipse. This was not resolved by either of these techniques and drove me crazy for two days.

I found one of the sites was showing the bug in STS 4.x which was fixed in 4.2. So, I want to try this as well. I tried to upgrade. Upgrade went well. But it didnt solve the problem. later I deleted the whole STS folder and used a fresh 4.4.0.RELEASE. Everything went smooth.. 😅😅

Nice picture, isn't it? I captured it at the Temple of tooth, Kandy (6 Jun 19)

Nice picture, isn’t it? I captured it at the Temple of tooth, Kandy (6 Jun 19)