Skip to content

Instantly share code, notes, and snippets.

View pmihalcin's full-sized avatar
🏠
Working from home

Patrik Mihalcin pmihalcin

🏠
Working from home
View GitHub Profile
#!/bin/awk -f
BEGIN {
# Print the squares from 1 to 10
for (i=1; i <= 10; i++) {
printf "The square of ", i, " is ", i*i;
}
# now end
exit;
}
@pmihalcin
pmihalcin / traverson.groovy
Created September 17, 2018 11:24
Traverson in MockMvc tests
// https://github.com/spring-projects/spring-hateoas/issues/733#issuecomment-420903832
def requestFactory = new MockMvcClientHttpRequestFactory(mvc)
def traverson = new Traverson(new URI("/api"), HAL_JSON)
traverson.setRestOperations(new RestTemplate(requestFactory))
traverson.follow(rel("merchants").withParameters(["page": "", "size": "", "sort": "", "projection": ""])).toObject("\$")
ParameterizedTypeReference<Resource<Merchant>> resourceParameterizedTypeReference = new ParameterizedTypeReference<Resource<Merchant>>() {
};
Resource<Merchant> itemResource = traverson.follow(rel("merchants").withParameters(["page": "", "size": "", "sort": "", "projection": ""]))
// Example how to use Traverson API
// please note, there is not MockMvc support yet
// fully-blown server is needed, RANDOM_PORT / DEFINED_PORT
@LocalServerPort
private int port;
def traverson = new Traverson(new URI("http://localhost:$port/api/"), HAL_JSON)
def object = traverson.follow(REL_SPEC_MER_CRITERIA, EXPORT_CSV_TEMPLATE_REL).toObject(String.class)
println object
@pmihalcin
pmihalcin / rabbit-publisher-confirms-transactions.md
Last active November 7, 2022 14:15
Rabbit publisher confirms or transactions

AMQP AcknowledgeMode

public static final AcknowledgeMode AUTO

  • Auto - the container will issue the ack/nack based on whether the listener returns normally, or throws an exception.
  • Do not confuse with RabbitMQ autoAck which is represented by NONE here.

The AUTO is involved in the commitIfNecessary() which is called only when your target listener returns normally

https://docs.spring.io/spring-amqp/reference/html/_reference.html#template-confirms

three first class citizens in Spring Integration
Message, Channel and Endpoint
Message - an independent, immutable container of data to transfer throughout business logic
Channel - we have to decouple business logic and be able to distribute the last one when it is requested
Channel has two ends: producer and consumer(s)
But at the same time there are many different channel implementation
and depending on that we determine what type of Endpoint (consumer) to declare
See ConsumerEndpointFactoryBean for more details
@pmihalcin
pmihalcin / how-to-requeue-in-spring-integration.java
Last active June 5, 2018 20:27
Requeue dead letter messages
// first example
@Bean
public IntegrationFlow flow(ConnectionFactory cf) {
return IntegrationFlows.from(Amqp.inboundAdapter(cf, "q").autoStartup(false))
.publishSubscribeChannel(s -> s
.subscribe(f -> f.transform("headers['x-death'] ?: '{}'")
.handle("deathCheckBean", "deathCheckMethod"))
.subscribe(f -> f.handle(System.out::println))) // main flow
.get();
}
@pmihalcin
pmihalcin / AmqpUtils.java
Created June 1, 2018 18:09
Amqp utils kung fu
package net.homecredit.mer.web
import org.springframework.amqp.rabbit.connection.Connection
import org.springframework.amqp.rabbit.connection.ConnectionFactory
import static java.lang.System.currentTimeMillis
import static net.homecredit.mer.web.WaitUtils.doWait
import static org.junit.Assert.fail
class AmqpUtils {
package net.homecredit.mer.web;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Patrik.Mihalcin on 29.03.2018
*/
public class GenericsBoundedWildcards {
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashSet;
import java.util.Set;
// billion-laughs-style DoS for java serialization
public class SerialDOS {
@RunWith(SpringRunner.class)
@SpringBootTest
public class DbCommitJmsRollback implements ApplicationContextAware {
private Lifecycle lifecycle;
@Autowired
private JmsTemplate jmsTemplate;
@Autowired