ozimov / spring-boot-email-tools

A set of services and tools for sending emails in a Spring Boot 1.5.x application using a Template Engine

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Fail to run Spring

Fraccaman opened this issue · comments

After configuring "spring-boot-mustache-email" as per docs, spring boot fail to start crashing with this error:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'priorityQueueSchedulerService' defined in URL [jar:file:/Users/Fraccaman/.m2/repository/it/ozimov/spring-boot-email-core/0.4.0/spring-boot-email-core-0.4.0.jar!/it/ozimov/springboot/templating/mail/service/defaultimpl/PriorityQueueSchedulerService.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'schedulerProperties': Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkState(ZLjava/lang/String;Ljava/lang/Object;Ljava/lang/Object;)V

Spring boot version: 1.5.1 RELEASE

@Fraccaman can you please provide more context?

@robertotru yes, I'm sorry I have accidentally sent the issue empty.If you need anything more, tell me. Thank!

Yeah, if you need the scheduler add the following properties

spring.mail.scheduler.enabled=true
spring.mail.scheduler.priorityLevels=5

(5 is just an example).

If you do not, just add

spring.mail.scheduler.enabled=false
spring.mail.scheduler.priorityLevels=1

In the following days I'm gonna release a minor fix version that defaults to false.

spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=MAIL
spring.mail.password=PASSWORD
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

spring.mail.scheduler.persistence.enabled=false
spring.mail.scheduler.persistence.redis.embedded=false
spring.mail.scheduler.persistence.redis.enabled=false
spring.mail.scheduler.enabled=false
spring.mail.scheduler.priorityLevels=1

with this configuration I'm still getting the same error.
Do you have any other idea?

No, without having more info on the project structure, pom, etc. I cannot. Everything looks fine!

Actually in pull request #26 I added an example project in which I send an email with attachment, and inline pictures using Mustache. Just have a look at the PR or clone from that branch.

I watched your pull request but I still can't find what I'm doing wrong.

These are my pom dependecies:

<dependencies>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>

		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>

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

		<dependency>
			<groupId>com.auth0</groupId>
			<artifactId>auth0-spring-security-api</artifactId>
			<version>0.3.3</version>
		</dependency>

        <dependency>
            <groupId>com.auth0</groupId>
            <artifactId>auth0</artifactId>
            <version>0.3.0</version>
        </dependency>

		<dependency>
			<groupId>org.apache.tika</groupId>
			<artifactId>tika-core</artifactId>
			<version>1.14</version>
		</dependency>

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

		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.5.0</version>
		</dependency>

		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>2.5.0</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>1.16.12</version>
		</dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.8.6</version>
        </dependency>

		<dependency>
			<groupId>org.springframework.security</groupId>
			<artifactId>spring-security-test</artifactId>
			<version>4.1.3.RELEASE</version>
		</dependency>

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

		<dependency>
				<groupId>net.sf.ehcache</groupId>
			 <artifactId>ehcache</artifactId>
			 <version>2.10.3</version>
		</dependency>

		<dependency>
			<groupId>com.h2database</groupId>
			<artifactId>h2</artifactId>
			<version>1.4.193</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-test</artifactId>
			<version>1.4.3.RELEASE</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>it.ozimov</groupId>
			<artifactId>spring-boot-mustache-email</artifactId>
			<version>0.4.0</version>
		</dependency>

	</dependencies>

and here is my implementation:

@Service
@Async
public class EmailSenderService {

    public final EmailService emailService;

    @Autowired
    public EmailSenderService(EmailService emailService) {
        this.emailService = emailService;
    }

    public void sendEmailWithTemplating(){
        Stream.of(new Cospirator("fraccg@usi.ch", "Gaius Cassius Longinus")).forEach(tyrannicida -> {
            Email email = null;
            try {
                email = DefaultEmail.builder()
                        .from(new InternetAddress("divus.iulius@mala-tempora.currunt", "Gaius Iulius Caesar"))
                        .to(Lists.newArrayList(new InternetAddress(tyrannicida.email, tyrannicida.name)))
                        .subject("Idus Martii")
                        .body("")//Empty body
                        .encoding(String.valueOf(Charset.forName("UTF-8"))).build();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            //Defining the model object for the given Freemarker template
            final Map<String, Object> modelObject = new HashMap<>();
            modelObject.put("tyrannicida", tyrannicida.name);

            try {
                emailService.send(email, "email.mustache", modelObject);
            } catch (CannotSendEmailException e) {
                e.printStackTrace();
            }
        });
    }

    private static class Cospirator {
        private String email;
        private String name;
        public Cospirator(final String email, final String name){
            this.email = email;
            this.name = name;
        }
    }
}

And the main is:

@SpringBootApplication
@EnableCaching
@EnableScheduling
@ComponentScan(basePackages = {"com.dubi", "it.ozimov.springboot.templating.mail"})
public class DubiApplication {

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

the file 'email.mustache' is inside the folder templates in resources.
Do you see anything wrong here? Thank again for your time.

I see... The problem is just in some application.properties, as reported in the exception.

Just try to replace:

spring.mail.scheduler.persistence.enabled=false
spring.mail.scheduler.persistence.redis.embedded=false
spring.mail.scheduler.persistence.redis.enabled=false
spring.mail.scheduler.enabled=false
spring.mail.scheduler.priorityLevels=1

with

spring.mail.scheduler.priorityLevels: 10
spring.mail.persistence.enabled: false
spring.mail.persistence.redis.embedded: false
spring.mail.persistence.redis.enabled: false
spring.mail.scheduler.persistenceLayer.desiredBatchSize: 200
spring.mail.scheduler.persistenceLayer.minKeptInMemory: 100
spring.mail.scheduler.persistenceLayer.maxKeptInMemory: 1000

The problem with this version is that the email scheduler creates a thread periodically waiting for something to be sent. In version 0.4.1 the scheduler becomes disabled by default and some properties will be refactored. I was supposed to create the release today, but I have an issue with the Pebble template engine.
I hope that config solves.

Thank for you time, but that config unfortunately is not solving the problem.
If you say that within the next days you will release the fixed version I can wait.

Thank again!

I'm creating a minor release, it will be available in a few hours (it really depends on the Maven Staging Environment). Use the properties you find in the README to get it work.

@Fraccaman did release 0.4.1 fixed your issue?

Closing due to inactivity.

I'm sorry but I couldn't answer during the past few days. Tomorrow I'm gonna test the new version and I'll give you a feedback. Thanks.

The new version is working perfectly. Thank you again for your support! 👍