http://theblasfrompas.blogspot.com.au/2017/03/run-spring-cloud-task-from-pivotal.html
Taking that same example I have used the Spring Cloud Connectors to persist the log output to a database table to avoid looking through log files to view the output. Few things have to change to make this happen as detailed below.
1. We need to change the manifest.yml to include a MySQL service instance as shown below
applications:
- name: springcloudtask-date
memory: 750M
instances: 1
no-route: true
health-check-type: none
path: ./target/springcloudtasktodaysdate-0.0.1-SNAPSHOT.jar
services:
- pmysql-test
env:
JAVA_OPTS: -Djava.security.egd=file:///dev/urando
2. Alter the project dependancies to include Spring Data JPA libraries to persist the log output to a table. Spring Cloud Connectors will automatically pick up the bound MySQL instance and connect for us when we push the application to PCF
https://github.com/papicella/SpringCloudTaskTodaysDate
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-task</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> </dependencies>
3. A Entity class, Spring JPA repository interface and a JPA task Configurer has been created for persisting the log output as shown in the code below.
TaskRunOutput.java
package pas.au.pivotal.pa.sct.demo; import javax.persistence.*; @Entity @Table (name = "TASKRUNOUTPUT") public class TaskRunOutput { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String output; public TaskRunOutput() { } public TaskRunOutput(String output) { this.output = output; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getOutput() { return output; } public void setOutput(String output) { this.output = output; } @Override public String toString() { return "TaskRunOutput{" + "id=" + id + ", output='" + output + '\'' + '}'; } }
TaskRepository.java
package pas.au.pivotal.pa.sct.demo; import org.springframework.data.jpa.repository.JpaRepository; public interface TaskRepository extends JpaRepository <TaskRun, Long> { }
JpaTaskConfigurer.java
package pas.au.pivotal.pa.sct.demo.configuration; import java.text.SimpleDateFormat; import java.util.Date; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import pas.au.pivotal.pa.sct.demo.TaskRunOutput; import pas.au.pivotal.pa.sct.demo.TaskRunRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.task.configuration.DefaultTaskConfigurer; import org.springframework.cloud.task.listener.annotation.BeforeTask; import org.springframework.cloud.task.repository.TaskExecution; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.stereotype.Component; import org.springframework.transaction.PlatformTransactionManager; @Component public class JpaTaskConfigurer extends DefaultTaskConfigurer { private static final Log logger = LogFactory.getLog(JpaTaskConfigurer.class); @Autowired private PlatformTransactionManager transactionManager; @Autowired private TaskRunRepository taskRunRepository; @Override public PlatformTransactionManager getTransactionManager() { if(this.transactionManager == null) { this.transactionManager = new JpaTransactionManager(); } return this.transactionManager; } @BeforeTask public void init(TaskExecution taskExecution) { String execDate = new SimpleDateFormat().format(new Date()); taskRunRepository.save(new TaskRunOutput("Executed at " + execDate)); logger.info("Executed at : " + execDate); } }
4. Now as per the previous blog execute the task and verify it completes without error. The screen shot below shows how the "Tasks" tab shows this
Note: You would need to PUSH the application to Pivotal Cloud Foundry before you can execute it which is shown on the original blog entry
5. Now if you follow this blog entry below you can deploy a Web Based interface for Pivotal MySQL instance to view the table and it's output
http://theblasfrompas.blogspot.com.au/2017/04/accessing-pivotal-mysql-service.html
With Pivotal MySQL*Web installed the output can be viewed as shown below.
No comments:
Post a Comment