uebung3
wip
This commit is contained in:
@@ -10,12 +10,34 @@
|
||||
<artifactId>ejb-server</artifactId>
|
||||
<packaging>ejb</packaging>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>jakarta.enterprise</groupId>
|
||||
<artifactId>jakarta.enterprise.cdi-api</artifactId>
|
||||
<version>4.1.0</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>jakarta.platform</groupId>
|
||||
<artifactId>jakarta.jakartaee-api</artifactId>
|
||||
<version>10.0.0</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>jakarta.ejb</groupId>
|
||||
<artifactId>jakarta.ejb-api</artifactId>
|
||||
<version>4.0.1</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish.jaxb</groupId>
|
||||
<artifactId>jaxb-runtime</artifactId>
|
||||
<version>4.0.5</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate.orm</groupId>
|
||||
<artifactId>hibernate-core</artifactId>
|
||||
<version>7.0.4.Final</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package org.example.demo.uebung3.aufgabe13;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.ejb.Stateless;
|
||||
import jakarta.mail.MessagingException;
|
||||
import jakarta.mail.Session;
|
||||
import jakarta.mail.Transport;
|
||||
import jakarta.mail.internet.InternetAddress;
|
||||
import jakarta.mail.internet.MimeMessage;
|
||||
import java.util.Date;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@Stateless
|
||||
public class Mailsender implements MailsenderLocal {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(Mailsender.class.getName());
|
||||
|
||||
@Resource(mappedName="java:/jboss/mail/uebung3")
|
||||
private Session mailSession;
|
||||
|
||||
public Mailsender() {
|
||||
}
|
||||
|
||||
public void sendMail(String recipient, String subject, String text) {
|
||||
try {
|
||||
jakarta.mail.Message message = new MimeMessage(mailSession);
|
||||
message.setSentDate(new Date());
|
||||
message.addRecipient(jakarta.mail.Message.RecipientType.TO, new InternetAddress(recipient));
|
||||
message.setSubject(subject);
|
||||
message.setText(text);
|
||||
Transport.send(message);
|
||||
}
|
||||
catch (MessagingException e) {
|
||||
LOG.log(Level.SEVERE, "Fehler beim Senden der E-Mail an " + recipient, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package org.example.demo.uebung3.aufgabe13;
|
||||
|
||||
import jakarta.ejb.Local;
|
||||
|
||||
@Local
|
||||
public interface MailsenderLocal {
|
||||
void sendMail(String to, String subject, String text);
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
package org.example.demo.uebung3.aufgabe13;
|
||||
|
||||
import jakarta.ejb.ActivationConfigProperty;
|
||||
import jakarta.ejb.EJB;
|
||||
import jakarta.ejb.MessageDriven;
|
||||
import jakarta.jms.JMSException;
|
||||
import jakarta.jms.Message;
|
||||
import jakarta.jms.MessageListener;
|
||||
import jakarta.jms.TextMessage;
|
||||
|
||||
@MessageDriven(
|
||||
activationConfig = {
|
||||
@ActivationConfigProperty(
|
||||
propertyName = "destinationType",
|
||||
propertyValue = "jakarta.jms.Queue"
|
||||
),
|
||||
@ActivationConfigProperty(
|
||||
propertyName = "destination",
|
||||
propertyValue = "java:/jms/queue/MyQueue"
|
||||
),
|
||||
@ActivationConfigProperty(
|
||||
propertyName = "acknowledgeMode",
|
||||
propertyValue = "Auto-acknowledge"
|
||||
)
|
||||
}
|
||||
)
|
||||
public class ReceiveQueueMessageAndSendAsMail implements MessageListener {
|
||||
|
||||
@EJB
|
||||
private MailsenderLocal mail;
|
||||
|
||||
@Override
|
||||
public void onMessage(Message message) {
|
||||
try {
|
||||
if (message instanceof TextMessage) {
|
||||
String text = ((TextMessage) message).getText();
|
||||
System.out.printf("=== Empfangene Nachricht: %s ===", text);
|
||||
|
||||
// Konfiguration für E-Mail
|
||||
String empfaenger = "linus.nagel@itc-studenten.de";
|
||||
String betreff = "JMS-Nachricht empfangen";
|
||||
mail.sendMail(empfaenger, betreff, text);
|
||||
|
||||
System.out.printf("E-Mail an %s versendet: %s", empfaenger, text);
|
||||
} else {
|
||||
System.out.printf("Unerwarteter Nachrichtentyp: %s", message.getClass().getName());
|
||||
}
|
||||
} catch (JMSException e) {
|
||||
System.out.println("Fehler beim Verarbeiten der Nachricht " + e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user